[concurrency-interest] producer-consumer POISON msg

Satyendra Gurjar sg at sgurjar.com
Mon Nov 29 12:02:49 EST 2010


Hello, I'm trying producer-consumer pattern as described in jcip 5.3
I'm putting POISON MSG for consumers to stop after finish consuming real msgs.
But what I'm seeing is consumers consumes POISON MSG before real msgs and
stops. Following is my code, please help me understand the behavior.

Thanks.


static final File POISON_MSG = new File("");

static void main(String[] args) throws Exception {

 int N_CONSUMERS = 10, BOUND = 1000;
 BlockingQueue<File> blockingqueue = new LinkedBlockingQueue<File>(BOUND);

 ArrayList<Thread> producers = new ArrayList<Thread>();
 for(String[] file : logfiles) { // producer threads
     Thread t = new Thread(new Producer(file[0], file[1], blockingqueue));
     t.start();
     producers.add(t);
 }

 Crawler crawler = new Crawler(); // Crawler is immutable

 for(int i=0; i < N_CONSUMERS; i++) { // consumer threads
     new Thread(new Consumer(blockingqueue, crawler)).start();
 }

 // wait for all producers to finish
 for(Thread p : producers) p.join();

 // then put N_CONSUMERS POISON_MSG for consumers to stop
 for(int i=0; i < N_CONSUMERS; i++) blockingqueue.put(POISON_MSG);

}


More information about the Concurrency-interest mailing list