Future in Java 8

In Java, the Callable interface was introduced in Java 5 as an alternative to existing Runnable interface. It wraps a task and pass it to a Thread or thread pool for asynchronous execution. The Callable represents an asynchronous computation, whose value is available through a Future object. All the code that needs to be executed asynchronously goes into the call() method. Callable is also a single abstract method type, so it can be used along with lambda expression on Java 8. Both Callable and Future are parametric types and can be used to wrap classes like Integer, String, or anything else.

In this section, we will understand what is Future in Java 8, its functionality, methods and implementation of Future.

Java 8 Future Interface

In Java 8, Future is an interface that belongs to java.util.concurrent package. It is used to represent the result of an asynchronous computation. Note that it is quite similar to JavaScript Promise.

Syntax:

Where V is the type of result return by the future.

Example of Future Interface

The most common example of Java Future is ExecutorService interface. It produces a Future (from some of their methods) object for tracking progress of one or more asynchronous task.

Methods of Future Interface

The interface provides the methods to check if the computation has completed or not, to wait for its completion, and to retrieve the result of the computation. The result can only get once the computation has completed, else block the computation until the result is ready. Once the task or computation is completed one cannot cancel the computation.

The interface provides the following methods.

MethodDescription
cancel()It tries to cancel the execution of the task.
get()The method waits if necessary, for the computation to complete, and then retrieves its result.
get()Waits if necessary, for at most the given time for the computation to complete, and then retrieves its result, if available.
isCancelled()It returns true if the task was cancelled before its completion.
isDone()It returns true if the task is completed.

Shortcoming of the Future Interface

There was some limitation of the Future interface that are as follows:

  • Using Future, the computation cannot be completed manually.
  • It does not notify once the commutation is completed.
  • There is a lack of mechanism like create stages of processing that are chained together and to run Future in parallel and combined their results.
  • It does not provide any exception handling constructs.

In order to overcome the above limitations, Java 8 introduced concrete Future implementations that provide the following features as follows:

Implementation

Before implementing the Future interface in a Java program, first we will have a look at the basic interfaces and classes from which the Future interface belongs.

Callable interface is an advanced version of the Runnable interface. It represents a task that returns a result and may throw an exception. Implement the call() method without any argument, if we want to use Callable interface.

In order to pass a Callable to a thread pool use the ExecutorService. The easiest way to create an ExecutorService is to use one of the factory methods of the Executors class. For concurrent execution, it chooses one thread and executes the Callable. It immediately returns a Future object that ensures to hold the result of computation once done. After that, invoke the get() method of the Future interface to get result of computation.

Java Future Example

The following Java program demonstrate how one can use Callable and Future together to implement asynchronous processing in Java.

FutureExample.java

Output:

I am in temp method
In call method of PrintString class MySQL
In call method of PrintString class Python
In call method of PrintString class Flutter
In call method of PrintString class Java
In call method of PrintString class Bash
In call method of PrintString class Go
1. Length of string Java is 4
2. Length of string Python is 6
3. Length of string Flutter is 7
4. Length of string MySQL is 5
5. Length of string Bash is 4
6. Length of string Go is 2





Latest Courses