[英]Termination in Producer Consumer multithreading Java
我正在使用多線程和Java並發功能進行一些練習。 我有1個生產者和4個消費者。 現在我的問題是:當我確定生產者已經在BlockingQueue中完成生產時,還有其他更聰明的方法來阻止消費者嗎? 現在,我在隊列中使用-1 Integer,但看起來非常簡單!! 謝謝
public class Exercise {
static class Producer implements Runnable {
int counter=0;
private BlockingQueue<Integer> queue;
Producer(BlockingQueue<Integer> q) {
queue = q;
}
public void run() {
try {
while (counter<100000000) {
queue.put(produce());
}
queue.put(new Integer(-1));
queue.put(new Integer(-1));
queue.put(new Integer(-1));
queue.put(new Integer(-1));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Integer produce() {
counter++;
return new Integer(counter);
}
}
static class Consumer implements Runnable {
private final BlockingQueue<Integer> queue;
private String name;
private long sum;
Consumer(BlockingQueue<Integer> q, String name) {
queue = q;
this.name=name;
sum=0;
}
public void run() {
try {
int x=0;
while (x>=0) {
x=queue.take();
if(x!=-1)sum+=x;
}
System.out.println(sum+" of "+ name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ExecutorService exec = Executors.newFixedThreadPool(6);
BlockingQueue<Integer> q =new LinkedTransferQueue<Integer>();
Producer p=new Producer(q);
Consumer c1 = new Consumer(q,"consumer1");
Consumer c2 = new Consumer(q,"consumer2");
Consumer c3 = new Consumer(q,"consumer3");
Consumer c4 = new Consumer(q,"consumer4");
exec.submit(p);
exec.submit(c1);
exec.execute(c2);
exec.submit(c3);
exec.execute(c4);
exec.shutdown();
}
}
您可以使用毒葯,但是使用這種毒葯的更可靠的方法是不將其從隊列中刪除(或將其放回隊列中),這種向生產者的方式不需要知道您有多少消費者有。
順便說一句,我會避免使用顯式拳擊,因為它比較冗長和緩慢。
代替
queue.put(new Integer(-1));
你可以寫
queue.put(-1);
甚至更好
static final int POISON_PILL = -1;
// on the producer
queue.put(POISON_PILL);
// on the consumer
while ((x = queue.take()) != POISON_PILL) {
sum += x;
queue.put(POISON_PILL);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.