5

More precisely : If I start async computation by calling submit(Callable<T> task) method on an ExecutorService (itself constructed with Executors.newCachedThreadPool()), I can wait for the computation to finish and retrieve the result by calling Future.get().

My question is : if the computation is already finished, what happens until I call get()? Does the thread is blocked until I retrieved the result? Does the result is stored and the thread assigned to another task? Something completely different?

Thanks in advance for you answers

1 Answers1

4

No, Thread is not blocked, it is returned to the pool. In general it is the get() calling thread dependant on worker, not the other way around. So if there is a result, return it, if not, wait until it will be available.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Thanks for you answer! Just to be sure I understand you correctly (sorry, English is not my mother tongue) : If I launch several threads for computations and retrieve the result only much later when all are finished, it's not a problem because finished tasks doesn't use any resources, right? – shadow_heart Mar 31 '15 at 18:33
  • @shadow_heart The result of the computation (=task) is stored within the `Future` object, so you can `get()` it whenever you want. `get()` only blocks the calling thread in case the computation has not finished yet. – isnot2bad Apr 01 '15 at 00:37