Future Interface in Java

Future is an interface in the Java language that is a part of the java.util.concurrent package. It serves as a symbol for the output of an asynchronously computation. The interface offers ways to determine whether a computation has finished, to wait for it to finish, and to receive the computation's result. That once task or calculation is finished, it cannot be undone.

Syntax:

Example of Java Future

The ExecutorService interface is the best illustration of a Java Future. It generates a Future object for tracking the advancement of one or even more asynchronous tasks (from a few of their methods).

Methods of the Future Interface

The interface offers the five following techniques:

MethodsDescription
cancle ()This Method tries to stop the task from being completed.
get ()The procedure receives its result after waiting, if necessary, for such computation to finish.
get ()if necessary, waits for the computations to finish for no longer than the specified amount of time before retrieving the result.
isCancelled ()If indeed the task was terminated before to completion, this method will return true.
isDone()If the task has been finished, this method will return true.

The Future interface had some flaws, including the following:

  • The calculation cannot be done manually when using Future.
  • That once commutation is complete, it doesn't notify.
  • It is impossible to make and combine its chain.

Java 8 provided CompletableFuture to get around the aforementioned restrictions.

Using Future in Asynchronous Programming

The Future, as we have already discussed, is the outcome of an asynchronously task. This Java Future interface offers the following two iterations of the get() method, both of which return an object, to receive the outcome of that asynchronous task. The return type could be a generic type, so keep that in mind. For instance:

The get() function will wait till the result is ready if we attempt to call it before the asynchronous job has finished.

The Future interface offers a different iteration of the get() function that accepts a time duration (in milliseconds) as an argument in order to address the aforementioned flaw. It signifies that the Future will be required to wait for a certain amount of time for the task to be finished before the outcome is made available. For instance:

Future will throw a TimeoutException if it does not receive a response within the allotted time.

Cancel an Asynchronous Task

Asynchronous tasks can also be stopped at any time by using the Future interface's cancel() method. For instance:

Check if an Asynchronous Task is Cancelled

The isCancelled() method of the Future interface can be used to determine whether or not the asynchronous job that Future represents has been cancelled. If the task is successfully cancelled, it returns true; otherwise, it returns false. For instance:

Example of Java Future

FutureExample.java

Output:

The thread is still running task in progress ..
The thread is still running task in progress ..
The thread is still running task in progress ..
The thread is still running task in progress ..
The thread is still running task in progress ..
Thread is completed and the task has completed ..
My name is manoj kumar





Latest Courses