Java Executor Service
The Java ExecutorService runs tasks on a pool of reusable threads, so you do not have to create and manage Thread objects yourself. You hand it work, and it decides which thread runs the work and when.
Creating a new thread for every small task is slow and can exhaust memory under load. An ExecutorService keeps a fixed set of worker threads, queues extra tasks, gives you results through Future objects, and shuts everything down cleanly. It lives in the java.util.concurrent package.
Why Use ExecutorService Instead of new Thread()
In Java Create Threads you started each task with new Thread(...).start(). That works for a few tasks, but it has limits.
new Thread() for each task | ExecutorService | |
|---|---|---|
| Thread creation | One new thread per task | Threads are created once and reused |
| Limit on threads | None: 10,000 tasks means 10,000 threads | You choose the pool size |
| Getting a result | Shared variables + join() | Future.get() |
| Exceptions | Lost unless you add a handler | Captured in the Future |
| Shutting down | Track and join every thread | shutdown(), close(), awaitTermination() |
How to Create an ExecutorService in Java
The Executors class has factory methods that create ready-made executors. The most common one is newFixedThreadPool(n), which keeps exactly n worker threads.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class FirstExecutor {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2); // two worker threads
for (int i = 1; i <= 4; i++) {
int orderNo = i;
executor.execute(() ->
System.out.println("Packing order " + orderNo + " on "
+ Thread.currentThread().getName()));
}
executor.shutdown(); // accept no new tasks
executor.awaitTermination(5, TimeUnit.SECONDS); // wait for queued tasks to finish
System.out.println("All orders packed");
}
}
Output (the order and thread assignment can change):
Packing order 1 on pool-1-thread-1
Packing order 2 on pool-1-thread-2
Packing order 3 on pool-1-thread-1
Packing order 4 on pool-1-thread-2
All orders packed
How it works:
newFixedThreadPool(2)creates a pool with two worker threads namedpool-1-thread-1andpool-1-thread-2.- Four tasks are given to
execute(). Two run straight away, and two wait in the executor's queue until a worker is free. shutdown()stops new tasks from being accepted but lets the queued ones finish.awaitTermination()blocksmainuntil they are done (or 5 seconds pass).
Executors Factory Methods
| Factory method | Threads | Typical use |
|---|---|---|
newFixedThreadPool(n) | Exactly n | General work with a known limit |
newSingleThreadExecutor() | 1 | Tasks that must run one at a time, in order |
newCachedThreadPool() | Grows and shrinks as needed | Many short tasks |
newScheduledThreadPool(n) | n, can delay or repeat tasks | Timers and periodic jobs |
newWorkStealingPool() | One per CPU core | Many small CPU-bound tasks |
newVirtualThreadPerTaskExecutor() (Java 21+) | One virtual thread per task | Large numbers of I/O-bound tasks |
Note: A pool must be shut down when you are finished with it. Its worker threads are not daemon threads, so a forgotten executor keeps the JVM running after
mainends.
ExecutorService execute() vs submit()
There are two ways to give a task to an executor. execute() comes from the Executor interface and returns nothing. submit() returns a Future, which lets you wait for the task, check its status, cancel it, or read its result.
execute(Runnable) | submit(Runnable) | submit(Callable<T>) | |
|---|---|---|---|
| Returns | void | Future<?> | Future<T> |
| Result | None | get() returns null | get() returns the value |
| Exception in task | Printed by the worker thread | Stored in the Future | Stored in the Future |
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class SubmitExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(2);
// submit a Runnable: no result, but we can wait for it
Future<?> backup = executor.submit(() -> {
sleepQuietly(300);
System.out.println("Backup finished");
});
// submit a Callable: it returns a value
Future<Integer> userCount = executor.submit(() -> {
sleepQuietly(200);
return 1_250;
});
System.out.println("Backup done yet? " + backup.isDone());
System.out.println("Users: " + userCount.get()); // waits for the result
backup.get(); // waits, returns null
System.out.println("Backup done now? " + backup.isDone());
executor.shutdown();
}
static void sleepQuietly(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Output:
Backup done yet? false
Users: 1250
Backup finished
Backup done now? true
How it works:
- The first lambda returns nothing, so it is a
Runnable. The second returns a value, so Java treats it as aCallable<Integer>. - Both tasks start at once on two different workers.
isDone()isfalseimmediately after submitting. userCount.get()blocksmainfor about 200 ms until the result is ready.backup.get()then blocks until the backup finishes.- Callables and
Futureare covered in depth in Java Callable Interface.
Shutting Down an ExecutorService
An executor has three shutdown methods. Picking the right one decides whether queued work finishes or is thrown away.
| Method | New tasks | Queued tasks | Running tasks | Blocks? |
|---|---|---|---|---|
shutdown() | Rejected | Still run | Finish normally | No |
shutdownNow() | Rejected | Removed and returned as a list | Interrupted | No |
awaitTermination(time, unit) | – | – | – | Yes, until done or timeout |
close() (Java 19+) | Rejected | Still run | Finish normally | Yes, until all done |
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ShutdownNowExample {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 1; i <= 5; i++) {
int taskNo = i;
executor.execute(() -> {
System.out.println("task " + taskNo + " started");
try {
Thread.sleep(1000);
System.out.println("task " + taskNo + " finished");
} catch (InterruptedException e) {
System.out.println("task " + taskNo + " interrupted");
}
});
}
Thread.sleep(300); // let task 1 start
List<Runnable> neverStarted = executor.shutdownNow();
boolean terminated = executor.awaitTermination(1, TimeUnit.SECONDS);
System.out.println("tasks never started: " + neverStarted.size());
System.out.println("terminated: " + terminated);
}
}
Output:
task 1 started
task 1 interrupted
tasks never started: 4
terminated: true
How it works:
- A single-thread executor runs task 1 while tasks 2–5 wait in the queue.
shutdownNow()interrupts the running task, which wakes fromsleep()with anInterruptedException. Tasks 2–5 are removed from the queue and returned as a list.awaitTermination()waits for the worker to stop, so "task 1 interrupted" is printed beforemainprints the totals.
Using try-with-resources (Java 19+)
Since Java 19, ExecutorService implements AutoCloseable. Its close() method calls shutdown() and then waits for all tasks to finish, so a try-with-resources block replaces the manual shutdown code.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TryWithResourcesExecutor {
public static void main(String[] args) {
try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
for (String city : new String[] {"Chennai", "Madurai", "Coimbatore"}) {
executor.submit(() -> System.out.println("Weather updated for " + city));
}
} // close(): no new tasks, then wait until every task is finished
System.out.println("All cities updated");
}
}
Output (the city order can change):
Weather updated for Chennai
Weather updated for Coimbatore
Weather updated for Madurai
All cities updated
How it works:
- The executor is created in the
try (...)header, so Java callsclose()automatically when the block ends. close()waits for all three tasks, so "All cities updated" is always printed last.- This is the cleanest pattern on modern Java. On Java 17 or older, use
shutdown()plusawaitTermination()in afinallyblock.
Running Many Tasks with invokeAll and invokeAny
invokeAll() runs a whole collection of Callable tasks and waits until all of them finish. It returns a list of Futures in the same order as the tasks. invokeAny() waits only for the first task that succeeds, returns its result, and cancels the rest.
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 InvokeAllAny {
static Callable<String> download(String mirror, long delayMs) {
return () -> {
Thread.sleep(delayMs); // simulate network time
return mirror;
};
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Callable<String>> mirrors = List.of(
download("mirror-mumbai", 300),
download("mirror-chennai", 100),
download("mirror-delhi", 200));
try (ExecutorService executor = Executors.newFixedThreadPool(3)) {
List<Future<String>> all = executor.invokeAll(mirrors); // waits for all three
for (Future<String> result : all) {
System.out.println("invokeAll result: " + result.get());
}
String fastest = executor.invokeAny(mirrors); // first one to succeed
System.out.println("invokeAny result: " + fastest);
}
}
}
Output:
invokeAll result: mirror-mumbai
invokeAll result: mirror-chennai
invokeAll result: mirror-delhi
invokeAny result: mirror-chennai
How it works:
invokeAll()returns only when all three downloads are complete (after about 300 ms). The results keep the order of the input list, not the order the tasks finished in.invokeAny()returns as soon as the 100 ms Chennai mirror finishes and cancels the other two.- Both methods also have versions with a timeout, for example
invokeAll(tasks, 2, TimeUnit.SECONDS).
Handling Exceptions in ExecutorService Tasks
If a task throws an exception, what you see depends on how it was started. With execute(), the worker thread prints the stack trace and is replaced by a new worker. With submit(), the exception is stored in the Future and only appears when you call get(), wrapped in an ExecutionException.
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TaskExceptions {
public static void main(String[] args) throws InterruptedException {
try (ExecutorService executor = Executors.newSingleThreadExecutor()) {
Future<Integer> quantity = executor.submit(() -> Integer.parseInt("12a"));
try {
System.out.println("Quantity: " + quantity.get());
} catch (ExecutionException e) {
System.out.println("Task failed: " + e.getCause());
}
}
}
}
Output:
Task failed: java.lang.NumberFormatException: For input string: "12a"
How it works:
Integer.parseInt("12a")throws aNumberFormatExceptioninside the worker thread.- Because the task was submitted, nothing is printed at that moment. The exception waits in the
Future. get()throws anExecutionException, andgetCause()returns the original exception.
Note: If you
submit()a task and never callget(), its exception is silently lost. Either callget(), or catch and log exceptions inside the task itself.
Real-World Example: Generating Order Reports in Parallel
An online shop must build reports for six orders. Each report needs a slow database call of about 200 ms. Run one after another, that takes 1.2 seconds; with a pool of three workers, it takes about 400 ms.
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 OrderReportGenerator {
record OrderReport(String orderId, int items, long totalPaise, String builtBy) { }
// Simulates loading an order from a database and building its report
static OrderReport buildReport(String orderId) throws InterruptedException {
Thread.sleep(200); // slow database call
int items = orderId.length() % 4 + 1;
long totalPaise = items * 49_900L;
return new OrderReport(orderId, items, totalPaise, Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<String> orderIds = List.of("ORD-101", "ORD-1022", "ORD-10333",
"ORD-104", "ORD-1055", "ORD-10666");
List<Callable<OrderReport>> jobs = new ArrayList<>();
for (String id : orderIds) {
jobs.add(() -> buildReport(id));
}
long start = System.currentTimeMillis();
long grandTotalPaise = 0;
try (ExecutorService reportPool = Executors.newFixedThreadPool(3)) {
List<Future<OrderReport>> results = reportPool.invokeAll(jobs);
for (Future<OrderReport> future : results) {
OrderReport report = future.get();
grandTotalPaise += report.totalPaise();
System.out.printf("%-9s items=%d total=Rs. %8.2f (%s)%n",
report.orderId(), report.items(), report.totalPaise() / 100.0, report.builtBy());
}
}
long elapsed = System.currentTimeMillis() - start;
System.out.printf("Grand total: Rs. %.2f%n", grandTotalPaise / 100.0);
System.out.println("Took about " + Math.round(elapsed / 100.0) * 100 + " ms (sequential: 1200 ms)");
}
}
Output (thread names and exact timing can vary):
ORD-101 items=4 total=Rs. 1996.00 (pool-1-thread-1)
ORD-1022 items=1 total=Rs. 499.00 (pool-1-thread-2)
ORD-10333 items=2 total=Rs. 998.00 (pool-1-thread-3)
ORD-104 items=4 total=Rs. 1996.00 (pool-1-thread-1)
ORD-1055 items=1 total=Rs. 499.00 (pool-1-thread-3)
ORD-10666 items=2 total=Rs. 998.00 (pool-1-thread-2)
Grand total: Rs. 6986.00
Took about 400 ms (sequential: 1200 ms)
How it works:
- One task per order. Each
CallablecallsbuildReport()for one order ID and returns anOrderReportrecord. - Parallel execution.
invokeAll()hands the six jobs to three workers. Three reports are built in the first 200 ms and three in the next 200 ms. - Ordered results. The list of
Futures matches the order oforderIds, so the printout is in order even though the work ran in parallel. - Automatic cleanup. The
try-with-resources block shuts the pool down and waits for it, so no worker threads are left running.
The next lesson, Java Thread Pools, looks inside these executors: how ThreadPoolExecutor decides when to create threads, queue tasks or reject them. For the basics of the tasks themselves, see Java Runnable Interface.