May I suggest checking for FutureTask :: isDone?
There will be something like this:
while(true) { if (normal.isDone()) { return normal.get(); } if (medium.isDone()) { return medium.get(); } if (huge.isDone()) { return huge.get(); } }
EDIT: You can cancel other tasks as soon as you have one result.
Using FutureTask :: get is not what you are looking for, as it will most likely always return the result of normal.get (), as the documentation already states that:
Waits if necessary to complete the calculation, and then retrieves its result.
To clarify the above: If you use FutureTask :: get the first FutureTask that you call, it will most likely block and wait for the result to return.
EDIT2:
Wrap this loop in a new Runnable executed by ExecutorService, passing the first result available to another method, or implement a callback and the wait will not be taken.
source share