Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Future.get() multiple invocations

Tags:

java

future

How does Java's Future.get() behave in the case where it is called multiple times after the task is completed? Does it return the the same result? Or does throw an ExecutionException again and again with the same exception if the computation failed? I can not find anything in the docs about it!

like image 407
Marco Servetto Avatar asked Feb 21 '15 17:02

Marco Servetto


1 Answers

You can call get() on a Future as often as you like, and it will only block if the task that produces the result has not finished yet.

If the task has already finished, it will just immediately return the result of the task.

If the task has failed with an exception, calling get() will throw an ExecutionException each time you call it.

like image 88
Jesper Avatar answered Sep 22 '22 01:09

Jesper