简体   繁体   中英

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

I have a list of Future tasks

   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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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