私は生産者消費者プログラムを作りました。これは、GUI (Swing または SWT) のないコア Java の単なるプログラムです。オブジェクトをキューに入れるプロデューサーが 1 つあります。また、その共有キューのすべてのオブジェクトにスタッフ (文字列など) を追加する必要があるコンシューマーもいくつかあります。したがって、すべてのコンシューマーは、共有キュー内のすべてのオブジェクトを処理する必要があります。この場合、すべての BookShelf には、「books」ArrayList 内のすべてのコンシューマーからのアイテムが必要です。消費者。
質問:スレッドを正しく終了するには、コンシューマーでどの条件を使用する必要がありますか? プログラムのコードの一部を次に示します。多分私は間違った方法でそれを実装しました。
キューのオブジェクトは次のとおりです。
public class BookShelf {
private int id;
private String name;
private int height;
private int weigh;
List<String> books = Collections.synchronizedList(new ArrayList<String>());
public BookShelf(int id, String name) {
this.id = id;
this.name = name;
}
public void addBook(String book) {
books.add(book);
}
public boolean eq(String book) {
synchronized (books) {
for (String b: books) {
if (b.equalsIgnoreCase(book)) {
return true;
}
}
}
return false;
}
other setters and getters..
}
プロデューサークラスは次のとおりです。
public class Producer implements Runnable {
private BlockingQueue myQueue;
public Producer(BlockingQueue myQueue) {
this.myQueue = myQueue;
}
public void run() {
for(int i=0; i<7; i++){
try {
System.out.println("Produced: " + i);
BookShelf myBookShelf = new BookShelf(i, "book #" + i);
myQueue.put(myBookShelf);
} catch (InterruptedException ex) {
//Proper handle
}
}
}
}
これは消費者クラスの1つです:
public class Consumer implements Runnable {
private BlockingQueue myQueue;
public Consumer(BlockingQueue myQueue) {
this.myQueue = myQueue; }
public void run() {
while(true){
try {
BookShelf tempBookShelf = (BookShelf) myQueue.take();
//eq() is my method to check if ArraList has a book.
if (tempBookShelf.eq("Abc book")) {
System.out.println("It already has book");
myQueue.put(tempBookShelf);
Thread.sleep(2000);
} else {
tempBookShelf.addBook("Abc book");
myQueue.put(tempBookShelf);
Thread.sleep(2000);
}
} catch (InterruptedException ex) {
//Proper handle
}
}
}
}
メインクラスは次のとおりです。
public class ProducerConsumerTest {
public static void main(String[] args) {
BlockingQueue sharedQueue = new LinkedBlockingQueue();
Thread prodThread = new Thread(new Producer(sharedQueue));
Thread consThread = new Thread(new Consumer(sharedQueue));
Thread consThread2 = new Thread(new Consumer2(sharedQueue));
prodThread.start();
consThread.start();
consThread2.start();
}
}