简体   繁体   English

如何在executorservice中继续监听任务

[英]How to keep listening for tasks in executorservice

Say I have a queue full of tasks which I need to submit to an executor service. 假设我有一个充满任务的队列,需要将其提交给执行者服务。 I want to process them in parallel. 我想并行处理它们。 That's cover. 就是封面

private ExecutorService service = Executors.newFixedThreadPool(5);

I recive 20 tasks to be executed, and all of there are processed as I expected in groups of 5. 我记录了20个要执行的任务,所有任务按我的预期(每5个一组)进行处理。

service.execute(tasks.poll());

But now I want to keep adding tasks to the queue and process them as I do now, but the service finish after the 20 original tasks. 但是现在我想继续将任务添加到队列中,并像现在一样处理它们,但是服务在完成20个原始任务之后就完成了。

How can I do this? 我怎样才能做到这一点?

       executor.awaitTermination()

这将等待所有任务完成,这应该在executorl.shutdown()之后使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM