java - how to deal with multiple worker threads that may create new work items -
i have queue contains work items , want have multiple threads work in parallel on items. when work item processed may result in new work items. problem have can't find solution on how determine if i'm done. worker looks that:
public class worker implements runnable { public void run() { while (true) { workitem item = queue.nextitem(); if (item != null) { processitem(item); } else { // queue empty, there may still other workers // processing items may result in new work items // how determine if work done? } } } }
this seems pretty simple problem i'm @ loss. best way implement that?
thanks
clarification: the worker threads have terminate once none of them processing item, long @ least 1 of them still working have wait because may result in new work items.
what using executorservice allow wait tasks finish: executorservice, how wait tasks finish
Comments
Post a Comment