[concurrency-interest] producer-consumer POISON msg

David Holmes davidcholmes at aapt.net.au
Wed Dec 1 02:41:34 EST 2010


Satyendra Gurjar writes:
> 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.

I can't see anything obviously wrong with your approach - though some of the
details are missing. If we wait until all producers are done before
inserting any POISON_MSGS in the queue then all the real messages must
appear before the poison ones and must be taken before them.

Can you reduce this to a full example (eg not using files and your
"crawler") and reproduce it? At a minimum can you should how producer and
consumer interact with the queue.

David Holmes
------------

> 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);
>
> }
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list