Otóż mam do rozwiązania zadanie:
Napisać program Author-Writer z wykładu przy użyciu blokujących kolejek.
Jako argumenty program otrzymuje napisy, które co sekundę ma generować Author.
Writer ma je wypisywać na konsoli.
Klasa Main ma następująca postać i nie można jej modyfikować
Oto treść programu z wykładu, o którym mowa w zadaniu:
// Klasa dla ustalania i pobierania tekstów
class Teksty {
String txt = null;
boolean newTxt = false;
// Metoda ustalająca tekst - wywołuje Autor
synchronized void setTextToWrite(String s) {
while (newTxt == true) {
try {
wait();
} catch(InterruptedException exc) {}
}
txt = s;
newTxt = true;
notifyAll();
}
// Metoda pobrania tekstu - wywołuje Writer
synchronized String getTextToWrite() {
while (newTxt == false) {
try {
wait();
} catch(InterruptedException exc) {}
}
newTxt = false;
notifyAll();
return txt;
}
}
// Klasa "wypisywacza"
class Writer extends Thread {
Teksty txtArea;
Writer(Teksty t) {
txtArea=t;
}
public void run() {
String txt = txtArea.getTextToWrite();
while(txt != null) {
System.out.println("-> " + txt);
txt = txtArea.getTextToWrite();
}
}
}
// Klasa autora
class Author extends Thread {
Teksty txtArea;
Author(Teksty t) {
txtArea=t;
}
public void run() {
String[] s = { "Pies", "Kot", "Zebra", "Lew", "Owca", "Słoń", null };
for (int i=0; i<s.length; i++) {
try { // autor zastanawia się chwilę co napisać
sleep((int)(Math.random() * 1000));
} catch(InterruptedException exc) { }
txtArea.setTextToWrite(s[i]);
}
}
}
// Klasa testująca
public class Koord {
public static void main(String[] args) {
Teksty t = new Teksty();
Thread t1 = new Author(t);
Thread t2 = new Writer(t);
t1.start();
t2.start();
}
}
To klasa main:
public class Main {
public static void main(String[] args) {
Author autor = new Author(args);
new Thread(autor).start();
new Thread(new Writer(autor)).start();
}
}
Ja natomiast napisałem coś takiego:
Klasa Author
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Author implements Runnable {
public final BlockingQueue sharedQueue;
private String[] s;
public Author(String[] string) {
this.s = string;
sharedQueue = new LinkedBlockingQueue();
}
@Override
public void run() {
for (int i=0; i<s.length-1; i++)
try {
java.lang.Thread.sleep(1000);
sharedQueue.put(s[i]);
} catch (InterruptedException ex) {
Logger.getLogger(Author.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Klasa Writer
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.BlockingQueue;
public class Writer implements Runnable {
private final BlockingQueue sharedQueue;
public Writer (Author auth) {
this.sharedQueue = auth.sharedQueue;
}
@Override
public void run() {
while(true){
try {
System.out.println("->\t" + sharedQueue.take());
} catch (InterruptedException ex) {
Logger.getLogger(Writer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Uważacie, że taki sposób rozwiązania jest poprawny? Co byście na moim miejscu poprawili?
Z góry dziękuje za wszystkie odpowiedzi,
Krzysztof