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

前端 未结 6 612
梦谈多话
梦谈多话 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:48

    There are a number of strategies you could use, but one simple one is to have a subclass of task that signals the end of the job. The producer doesn't send this signal directly. Instead, it enqueues an instance of this task subclass. When one of your consumers pulls off this task and executes it, that causes the signal to be sent.

提交回复
热议问题