Future and FutureTask in java - GeeksforGeeks

25 Jun, 2021
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.
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. 
Java
// Java program do two FutureTask// using Runnable Interface import java.util.concurrent.*;import java.util.logging.Level;import java.util.logging.Logger; class MyRunnable implements Runnable {     private final long waitTime;     public MyRunnable(int timeInMillis)    {        this.waitTime = timeInMillis;    }     @Override    public void run()    {        try {            // sleep for user given millisecond            // before checking again            Thread.sleep(waitTime);             // return current thread name            System.out.println(Thread                                   .currentThread()                                   .getName());        }         catch (InterruptedException ex) {            Logger                .getLogger(MyRunnable.class.getName())                .log(Level.SEVERE, null, ex);        }    }} // Class FutureTaskExample execute two future taskclass FutureTaskExample {     public static void main(String[] args)    {        // create two object of MyRunnable class        // for FutureTask and sleep 1000, 2000        // millisecond before checking again        MyRunnable myrunnableobject1 = new MyRunnable(1000);        MyRunnable myrunnableobject2 = new MyRunnable(2000);         FutureTask<String>            futureTask1 = new FutureTask<>(myrunnableobject1,                                           "FutureTask1 is complete");        FutureTask<String>            futureTask2 = new FutureTask<>(myrunnableobject2,                                           "FutureTask2 is complete");         // create thread pool of 2 size for ExecutorService        ExecutorService executor = Executors.newFixedThreadPool(2);         // submit futureTask1 to ExecutorService        executor.submit(futureTask1);         // submit futureTask2 to ExecutorService        executor.submit(futureTask2);         while (true) {            try {                 // if both future task complete                if (futureTask1.isDone() && futureTask2.isDone()) {                     System.out.println("Both FutureTask Complete");                     // shut down executor service                    executor.shutdown();                    return;                }                 if (!futureTask1.isDone()) {                     // wait indefinitely for future                    // task to complete                    System.out.println("FutureTask1 output = "                                       + futureTask1.get());                }                 System.out.println("Waiting for FutureTask2 to complete");                 // Wait if necessary for the computation to complete,                // and then retrieves its result                String s = futureTask2.get(250, TimeUnit.MILLISECONDS);                 if (s != null) {                    System.out.println("FutureTask2 output=" + s);                }            }             catch (Exception e) {                Sysmtem.out.println("Exception: " + e);            }        }    }}
Output: 
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
Reference:  
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/FutureTask.html
 
Java-Class and Object
java-interfaces
Technical Scripter 2018
Java
Technical Scripter
Java-Class and Object
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
Initialize an ArrayList in Java
How to iterate any Map in Java
Overriding in Java
LinkedList in Java
Collections in Java
Queue Interface In Java