The use of Future in java
Future is an interface introduced in java 1.5 that can be used to get asynchronous results easily. This article will explain how to use Future through specific examples.
Creating Future
As mentioned above, Future represents the result of asynchronous execution, which means that when the asynchronous execution is finished, the returned result will be saved in Future.
So when do we use Future? Generally speaking, when we execute a long-running task, using Future allows us to temporarily deal with other tasks, and then return the result when the long task is finished.
The scenarios where Future is often used are: 1. computationally intensive scenarios, 2. handling large amounts of data, 3. remote method calls, etc.
Next, we will use the ExecutorService to create a Future.
|
|
The above is a submit method defined in the ExecutorService, which takes a Callable parameter and returns a Future.
We use a thread to compute a square operation.
The submit takes a Callable parameter, and the Callable needs to implement a call method and return the result. Here we use lamaba expressions to simplify this process.
Get the result from Future
We have created Future above, next we look at how to get the value of Future.
First we use Future.isDone() to determine if this asynchronous operation is finished, if it is we can directly call futureOne.get() to get the result of Futre.
Here futureOne.get() is a blocking operation, it will wait until the asynchronous execution is finished before returning the result.
If we don’t want to wait, future provides a method with time.
|
|
If there is still a return from Future at the end of the wait time, a TimeoutException is thrown.
Cancel Future
If we submit an asynchronous program but want to cancel it, then we can do this.
Future.cancel(boolean) Pass in a boolean argument to choose whether to interrupt the running task.
If we cancel and then call the get() method again, a CancelationException will be thrown.
Running in a multi-threaded environment
If there are two computation tasks, first look at the result of running under single thread.
|
|
Because we created the single thread pool by Executors.newSingleThreadExecutor(). So the result of the run is as follows.
|
|
If we use Executors.newFixedThreadPool(2) to create a multi-threaded pool, we get the following result.