Java Future

This post explains the role of future in asynchronous programming in java

4/27/20231 min read

Future is an interface in java which belongs to java.util.concurrent package.It was introduced in Java 1.5 .It is used to retrieve the result of an asynchronous computation. It provides method to check whether the computation is complete or not.

It has get() method which is used to retrieve the result of computation and if the computation is not complete it blocks until it gets completed.

Different methods in future interface.

  1. V get() :- It is used to retrieve the result of execution.

  2. boolean cancel(boolean mayInterruptIfRunning):-It is used to cancel the execution.It returns true if the task is cancelled otherwise false.Based on the value of mayInterruptIfRunning the thread will be interrupted.If mayInterruptIfRunning is true then the thread will be interrupted even if it is executing some task.

  3. boolean isDone():It is used to check whether the computation is complete or not .It returns true if the task is complete otherwise it returns false.

  4. boolean isCancelled():-It returns true if the task was cancelled successfully before the normal completion of the task.