ExecutorService
Starting a thread per task is a poor plan: a thread costs a stack, and a program with thousands of short tasks spends more time making threads than doing work. An ExecutorService is a pool of threads that takes tasks from a queue, and it is the way a program runs concurrent work in Java.
A fixed thread pool
newFixedThreadPool(3) makes a pool of at most three threads. submit takes a task and answers a Future immediately, so the loop of six submissions returns at once and the tasks run while the main thread is still submitting:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class Pool {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(3);
List<Future<Integer>> futures = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
int task = i;
Callable<Integer> work = () -> task * task;
futures.add(pool.submit(work));
}
for (int i = 0; i < futures.size(); i++) {
System.out.println("task " + (i + 1) + " -> " + futures.get(i).get());
}
pool.shutdown();
System.out.println("shutdown called: " + pool.isShutdown());
System.out.println("terminated: " + pool.awaitTermination(1, TimeUnit.SECONDS));
}
}
javac -Xlint:all Pool.java
java Pool
task 1 -> 1
task 2 -> 4
task 3 -> 9
task 4 -> 16
task 5 -> 25
task 6 -> 36
shutdown called: true
terminated: true
The squares are in the order of the tasks because the program prints them from the list, not from the tasks: a task may finish early and its number still prints in its place. get on a future waits until the task is done and answers its value, so the six lines are in order by construction.
shutdown and awaitTermination
A pool keeps its threads alive for the next task, and a JVM with a live pool does not exit on its own. shutdown says no new task will be submitted and lets the queued ones finish, and awaitTermination waits for the pool to become empty. A program that forgets the two hangs at the end of main.
shutdownNow is the other choice: it interrupts the running tasks and returns the queued ones as a list. It is the tool of a program that must stop, such as a server on a signal, and it needs the tasks to notice an interrupt, which is the one part of this API that a task has to cooperate with.
invokeAll
A list of tasks submitted together is invokeAll, which waits for all of them and answers the futures in the order the tasks were given. The program computes a word and its length and sums the lengths:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Invoke {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);
List<Callable<String>> tasks = new ArrayList<>();
for (String word : List.of("pear", "fig", "apple")) {
tasks.add(() -> word.toUpperCase() + ":" + word.length());
}
List<Future<String>> results = pool.invokeAll(tasks);
int total = 0;
for (Future<String> result : results) {
String text = result.get();
System.out.println(text);
total += Integer.parseInt(text.substring(text.indexOf(':') + 1));
}
System.out.println("total letters: " + total);
Future<String> broken = pool.submit(() -> {
throw new IllegalStateException("task failed");
});
try {
broken.get();
} catch (ExecutionException e) {
System.out.println("caught: " + e.getCause().getMessage());
}
pool.shutdown();
}
}
javac -Xlint:all Invoke.java
java Invoke
PEAR:4
FIG:3
APPLE:5
total letters: 12
caught: task failed
The three results are in the order of the input although two threads ran them, which is what invokeAll promises. The total is the sum of the three lengths, 4 + 3 + 5, and the order of the pipeline is the order of the words in the list.
A task that fails
The last lines show the failure of a task. The exception the task threw is not thrown by submit, which has already returned; it is stored in the future, and get wraps it in an ExecutionException whose cause is the original. That is the answer to the question lesson 114 left open: a failure of a thread arrives through the future, with its class, its message and its trace.
The catch prints e.getCause().getMessage() and not e.getMessage(), because the message of the wrapper is the class of the cause, which is less useful than the sentence the task wrote.
One thread at a time
newSingleThreadExecutor is a pool of one, and it is the smallest way to run tasks in the order they were submitted without the caller waiting for each:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Serial {
public static void main(String[] args) throws InterruptedException {
ExecutorService pool = Executors.newSingleThreadExecutor();
int[] order = new int[3];
for (int i = 0; i < 3; i++) {
int task = i;
pool.execute(() -> order[task] = task + 1);
}
pool.shutdown();
pool.awaitTermination(1, TimeUnit.SECONDS);
System.out.println("order: " + order[0] + " " + order[1] + " " + order[2]);
System.out.println("single thread pool ran them one at a time");
}
}
javac -Xlint:all Serial.java
java Serial
order: 1 2 3
single thread pool ran them one at a time
The result would be the same with three threads, because each task writes its own slot; what the single thread guarantees is that the tasks do not overlap. That is the property a program needs when the tasks share a resource that is not thread safe, and it is cheaper than making the resource safe.
Exercises
- Change the pool of Pool to one thread and confirm the results are unchanged.
- Shut the pool down without awaitTermination and describe what the last line prints.
- Submit a task that sleeps and cancel its future with cancel(true).
- Use invokeAll with tasks that take different times and print the results in submission order.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
submitanswer?Answer
A Future, which is a handle to the result the task will produce
The call returns before the task runs, so it can only answer a handle.
-
What does
shutdowndo?Answer
Refuses new tasks and lets the queued ones finish
The graceful stop is shutdown, and shutdownNow is the interrupting one.
-
How does the failure of a task reach the caller?
Answer
As the cause of an ExecutionException from get
The future stores the outcome, success or failure, and get rethrows the failure wrapped.
-
What does this program print?
import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Q1 { public static void main(String[] args) throws Exception { ExecutorService pool = Executors.newFixedThreadPool(2); Future<Integer> future = pool.submit(() -> 6 * 7); System.out.println(future.get()); pool.shutdown(); System.out.println(pool.isShutdown()); } }Answer
42 / true
The task returns 42 and the pool reports the shutdown that was called.
-
Why does a pool keep its threads alive?
Answer
A thread is expensive to create, and the next task reuses it
Reuse is the point of a pool, and it is why the program has to shut it down.
-
What does
invokeAllpromise about the futures?Answer
They are in the order of the tasks, and all tasks have finished
The method waits for the whole batch and keeps the input order.
-
What is
newSingleThreadExecutorfor?Answer
Running tasks one at a time, in submission order, without the caller waiting
It is the serial queue a program uses when the work shares one resource.
-
What does this program print?
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Q2 { public static void main(String[] args) throws Exception { ExecutorService pool = Executors.newSingleThreadExecutor(); Future<String> future = pool.submit(() -> "value"); System.out.println(future.get()); System.out.println(future.isDone()); pool.shutdown(); } }Answer
value / true
get waited for the task, so by the time it returns the future is done.
-
What is the cause of an ExecutionException?
Answer
The exception the task threw
The wrapper carries the task failure as its cause, which is what the program prints.
-
Why does a program need awaitTermination after shutdown?
Answer
shutdown only asks, and the caller may want to wait for the tasks to finish
The two calls are the ask and the wait, and a program often needs both.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev