1. Overview
In this tutorial, weβll learn about Future. An interface thatβs been around since Java 1.5, it can be quite useful when working with asynchronous calls and concurrent processing.
2. Creating Futures
Simply put, the Future class represents a future result of an asynchronous computation. This result will eventually appear in the Future after the processing is complete.
Letβs see how to write methods that create and return a Future instance.
Long running methods are good candidates for asynchronous processing and the Future interface because we can execute other processes while weβre waiting for the task encapsulated in the Future to complete.
Some examples of operations that would leverage the async nature of Future are:
- computational intensive processes (mathematical and scientific calculations)
- manipulating large data structures (big data)
- remote method calls (downloading files, HTML scrapping, web services)
2.1. Implementing Futures With FutureTask
For our example, weβre going to create a very simple class that calculates the square of an Integer. This definitely doesnβt fit the long-running methods category, but weβre going to put a Thread.sleep() call to it so that it lasts 1 second before completing:
public class SquareCalculator {
private ExecutorService executor
= Executors.newSingleThreadExecutor();
public Future<Integer> calculate(Integer input) {
return executor.submit(() -> {
Thread.sleep(1000);
return input * input;
});
}
}
The bit of code that actually performs the calculation is contained within the call() method, and supplied as a lambda expression. As we can see, thereβs nothing special about it, except for the sleep() call mentioned earlier.
It gets more interesting when we direct our attention to the use of Callable and ExecutorService.
Callable is an interface representing a task that returns a result, and has a single call() method. Here weβve created an instance of it using a lambda expression.
Creating an instance of Callable doesnβt take us anywhere; we still have to pass this instance to an executor that will take care of starting the task in a new thread, and give us back the valuable Future object. Thatβs where ExecutorService comes in.
There are a few ways we can access an ExecutorService instance, and most of them are provided by the utility class Executorsβ static factory methods. In this example, we used the basic newSingleThreadExecutor(), which gives us an ExecutorService capable of handling a single thread at a time.
Once we have an ExecutorService object, we just need to call submit(), passing our Callable as an argument. Then submit() will start the task and return a FutureTask object, which is an implementation of the Future interface.
3. Consuming Futures
Up to this point, weβve learned how to create an instance of Future.
In this section, weβll learn how to work with this instance by exploring all the methods that are part of Futureβs API.
3.1. Using isDone() and get() to Obtain Results
Now we need to call calculate(), and use the returned Future to get the resulting Integer. Two methods from the Future API will help us with this task.
Future.isDone() tells us if the executor has finished processing the task. If the task is complete, it will return true; otherwise, it returns false.
The method that returns the actual result from the calculation is Future.get(). We can see that this method blocks the execution until the task is complete. However, this wonβt be an issue in our example because weβll check if the task is complete by calling isDone().
By using these two methods, we can run other code while we wait for the main task to finish:
Future<Integer> future = new SquareCalculator().calculate(10);
while(!future.isDone()) {
System.out.println("Calculating...");
Thread.sleep(300);
}
Integer result = future.get();
In this example, weβll write a simple message on the output to let the user know the program is performing the calculation.
The method get() will block the execution until the task is complete. Again, this wonβt be an issue because in our example, get() will only be called after making sure that the task is finished. So in this scenario, future.get() will always return immediately.
Itβs worth mentioning that get() has an overloaded version that takes a timeout and a TimeUnit as arguments:
Integer result = future.get(500, TimeUnit.MILLISECONDS);
The difference between get(long, TimeUnit) and get() is that the former will throw a TimeoutException if the task doesnβt return before the specified timeout period.
3.2. Canceling a Future With cancel()
Suppose we triggered a task, but for some reason, we donβt care about the result anymore. We can use Future.cancel(boolean) to tell the executor to stop the operation and interrupt its underlying thread:
Future<Integer> future = new SquareCalculator().calculate(4);
boolean canceled = future.cancel(true);
Our instance of Future, from the code above, will never complete its operation. In fact, if we try to call get() from that instance, after the call to cancel(), the outcome will be a CancellationException. Future.isCancelled() will tell us if a Future was already cancelled. This can be quite useful to avoid getting a CancellationException.
Itβs also possible that a call to cancel() fails. In that case, the returned value will be false. Itβs important to note that cancel() takes a boolean value as an argument. This controls whether the thread executing the task should be interrupted or not.
4. More Multithreading With Thread Pools
Our current ExecutorService is single threaded, since it was obtained with the Executors.newSingleThreadExecutor. To highlight this single thread, letβs trigger two calculations simultaneously:
SquareCalculator squareCalculator = new SquareCalculator();
Future<Integer> future1 = squareCalculator.calculate(10);
Future<Integer> future2 = squareCalculator.calculate(100);
while (!(future1.isDone() && future2.isDone())) {
System.out.println(
String.format(
"future1 is %s and future2 is %s",
future1.isDone() ? "done" : "not done",
future2.isDone() ? "done" : "not done"
)
);
Thread.sleep(300);
}
Integer result1 = future1.get();
Integer result2 = future2.get();
System.out.println(result1 + " and " + result2);
squareCalculator.shutdown();
Now letβs analyze the output for this code:
calculating square for: 10
future1 is not done and future2 is not done
future1 is not done and future2 is not done
future1 is not done and future2 is not done
future1 is not done and future2 is not done
calculating square for: 100
future1 is done and future2 is not done
future1 is done and future2 is not done
future1 is done and future2 is not done
100 and 10000
Itβs clear that the process isnβt parallel. We can see that the second task only starts once the first task is complete, making the whole process take around 2 seconds to finish.
To make our program really multi-threaded, we should use a different flavor of ExecutorService. Letβs see how the behavior of our example changes if we use a thread pool provided by the factory method Executors.newFixedThreadPool():
public class SquareCalculator {
private ExecutorService executor = Executors.newFixedThreadPool(2);
//...
}
With a simple change in our SquareCalculator class, we now have an executor which is able to use 2 simultaneous threads.
If we run the exact same client code again, weβll get the following output:
calculating square for: 10
calculating square for: 100
future1 is not done and future2 is not done
future1 is not done and future2 is not done
future1 is not done and future2 is not done
future1 is not done and future2 is not done
100 and 10000
This is looking much better now. We can see that the 2 tasks start and finish running simultaneously, and the whole process takes around 1 second to complete.
There are other factory methods that can be used to create thread pools, like Executors.newCachedThreadPool(), which reuses previously used Threads when theyβre available, and Executors.newScheduledThreadPool(), which schedules commands to run after a given delay.
For more information about ExecutorService, read our article dedicated to the subject.
5. Overview of ForkJoinTask
ForkJoinTask is an abstract class which implements Future, and is capable of running a large number of tasks hosted by a small number of actual threads in ForkJoinPool.
In this section, weβll quickly cover the main characteristics of ForkJoinPool. For a comprehensive guide about the topic, check out our Guide to the Fork/Join Framework in Java.
The main characteristic of a ForkJoinTask is that it will usually spawn new subtasks as part of the work required to complete its main task. It generates new tasks by calling fork(), and it gathers all results with join(), thus the name of the class.
There are two abstract classes that implement ForkJoinTask: RecursiveTask, which returns a value upon completion, and RecursiveAction, which doesnβt return anything. As their names imply, these classes are to be used for recursive tasks, such as file-system navigation or complex mathematical computation.
Letβs expand our previous example to create a class that, given an Integer, will calculate the sum squares for all of its factorial elements. So for instance, if we pass the number 4 to our calculator, we should get the result from the sum of 4Β² + 3Β² + 2Β² + 1Β², which is 30.
First, we need to create a concrete implementation of RecursiveTask and implement its compute() method. This is where weβll write our business logic:
public class FactorialSquareCalculator extends RecursiveTask<Integer> {
private Integer n;
public FactorialSquareCalculator(Integer n) {
this.n = n;
}
@Override
protected Integer compute() {
if (n <= 1) {
return n;
}
FactorialSquareCalculator calculator
= new FactorialSquareCalculator(n - 1);
calculator.fork();
return n * n + calculator.join();
}
}
Notice how we achieve recursiveness by creating a new instance of FactorialSquareCalculator within compute(). By calling fork(), a non-blocking method, we ask ForkJoinPool to initiate the execution of this subtask.
The join() method will return the result from that calculation, to which weβll add the square of the number weβre currently visiting.
Now we just need to create a ForkJoinPool to handle the execution and thread management:
ForkJoinPool forkJoinPool = new ForkJoinPool();
FactorialSquareCalculator calculator = new FactorialSquareCalculator(10);
forkJoinPool.execute(calculator);
6. Conclusion
In this article, we comprehensively explored the Future interface, touching on all of its methods. We also learned how to leverage the power of thread pools to trigger multiple parallel operations. The main methods from the ForkJoinTask class, fork() and join(), were briefly covered as well.
We have many other great articles on parallel and asynchronous operations in Java. Here are three of them that are closely related to the Future interface, some of which are already mentioned in the article:
The code backing this article is available on GitHub. Once you're
logged in as a Baeldung Pro Member, start learning and coding on the project.