Prerequisite: Future and callable
A Future interface provides methods to check if the computation is complete, to wait for its completion and to retrieve the results of the computation. The result is retrieved using Future’s get() method when the computation has completed, and it blocks until it is completed.
Future and FutureTask both are available in java.util.concurrent package from Java 1.5.
- FutureTask implementation Future interface and RunnableFuture Interface, means one can use FutureTask as Runnable and can be submitted to ExecutorService for execution.
- When one call Future.submit() Callable or Runnable objects then most of time ExecutorService creates FutureTask, and one can create it manually also.
- FutureTask acts like a latch.
- Computation represent by FutureTask is implemented with Callable interface.
- It implements Future or Callable interface.
- Behaviour of get() method depends on the state of the task. If tasks are not completed get() method waits or blocks till the task is completed. Once task completed, it returns the result or throws an ExecutionException.
An example of using Future is working with Thread pools. When one submit a task to ExecutorService which is take a long running time, then it returns a Future object immediately. This Future object can be used for task completion and getting result of computation.
Examples: Create two task. After one is completely executed, then after waiting 2000 millisecond, second task is being executed
Note: Online IDE does not work properly on sleep() method.
FutureTask1 output=FutureTask1 is complete Waiting for FutureTask2 to complete Waiting for FutureTask2 to complete Waiting for FutureTask2 to complete Waiting for FutureTask2 to complete FutureTask2 output=FutureTask2 is complete Both FutureTask Complete
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Callable and Future in Java
- Is the future with snake(Python) or Coffee(Java) ?
- Future of home automation
- 5G: The Future Of Wireless Networks?
- Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
- Java.util.TreeMap.firstEntry() and firstKey() in Java
- Java.util.TreeMap.containskey() and containsValue() in Java
- Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
- Java.util.TreeMap.put() and putAll() in Java
- Java.util.TreeMap.floorEntry() and floorKey() in Java
- Java Swing | Translucent and shaped Window in Java
- Difference between Core Java and Advanced Java
- Difference between a Java Application and a Java Applet
- Difference between Java IO and Java NIO
- Difference between Java and Core Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Similarities and Difference between Java and C++
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.