Blocking queue and multi-threaded consumer, how to know when to stop

前端 未结 6 598
梦谈多话
梦谈多话 2020-12-04 06:53

I have a single thread producer which creates some task objects which are then added into an ArrayBlockingQueue (which is of fixed size).

I also start a

6条回答
  •  执笔经年
    2020-12-04 07:56

    I had to use a multi-threaded producer and a multi-threaded consumer. I ended up with a Scheduler -- N Producers -- M Consumers scheme, each two communicate via a queue (two queues total). The Scheduler fills the first queue with requests to produce data, and then fills it with N "poison pills". There is a counter of active producers (atomic int), and the last producer that receives the last poison pill sends M poison pills to the consumer queue.

提交回复
热议问题