简体   繁体   English

等待来自Java执行程序的至少一个结果而不用繁忙等待

[英]Wait for at least one result from Java executor without busy waiting

I have a list of Future tasks 我有一个Future任务列表

   futureList.add(executor.submit(new Callable(someList)));

and while those are getting executed I want to get resulting items out of it. 当那些被执行时,我想从中得到结果物品。 But how can I make it without looping all the time through it and checking if the futuretask is done, and then getting its result? 但是如何在没有循环的情况下完成它并检查futuretask是否完成,然后获得结果呢?

for (int i = 0; i < futureList.size(); i++) {
                    if (futureList.get(i).isDone()) {
    .....
}}

I thought about making some additional notify-wait structure but still I would have to know which thread finished so it doesn't help me avoid looping all the time. 我考虑过制作一些额外的通知等待结构,但我仍然必须知道哪个线程完成了所以它不能帮助我避免一直循环。 Any advice? 有什么建议?

使用ExecutorCompletionService,它完全是为此目的而设计的,请参阅API http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorCompletionService.html

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

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