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

concurrency, future, futuretask, java, multithreading

Solution

Use ExecutorCompletionService, it is designed exactly for this purpose, see API http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorCompletionService.html

Problem

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?

Original source