Future in Java 8In 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 InterfaceIn 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 InterfaceThe 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 InterfaceThe 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.
Shortcoming of the Future InterfaceThere was some limitation of the Future interface that are as follows:
In order to overcome the above limitations, Java 8 introduced concrete Future implementations that provide the following features as follows:
ImplementationBefore 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 ExampleThe 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 Next TopicHow to Set Timer in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India