Skip to content
javahacks.dev
Concurrency

Java Threads Explained From Thread to ExecutorService

Learn how to start threads, wait for them, share results with futures and move to executors and virtual threads, with the rules that keep code correct.

6 min read
Summarize in AI
Flat illustration of threads sharing work across lanes, a lock and a pool, with the OpenJDK, IntelliJ IDEA, Linux and Spring logos.

Concurrency in Java begins with one class and ends with a toolbox. This post starts at the beginning, a single thread, and climbs to executors and virtual threads, with the rule that matters at each step. The examples run on Java 25. The later steps are in the memory model and volatile vs synchronized vs atomic.

A thread runs a task

java
Thread t = new Thread(() -> System.out.println("running in " + Thread.currentThread().getName()));
t.start();
t.join();

The lambda is a Runnable. start() asks the JVM to create an operating system thread and call run() on it. Calling t.run() directly would just run it in the current thread, which is a common mistake. join() blocks the current thread until t finishes. The thread name is Thread-0 by default; give it a meaningful one with new Thread(task, "importer"), because names appear in thread dumps.

An uncaught exception in a thread kills that thread only and prints a stack trace to the standard error; the rest of the program continues. Set an UncaughtExceptionHandler or catch inside the task.

Why a pool

Creating a thread costs time and memory (a stack of about a megabyte of address space by default), and starting a thousand of them is a bad idea. An ExecutorService keeps a pool of threads and feeds them tasks:

java
ExecutorService pool = Executors.newFixedThreadPool(4);

Future<Integer> answer = pool.submit(() -> 6 * 7);
System.out.println(answer.get());       // prints 42

pool.shutdown();

submit takes a Callable (which returns a value and may throw) and returns a Future. get() waits for the result and rethrows task failures as ExecutionException. Always shut the executor down: a non-daemon pool thread keeps the JVM alive after main ends. Since Java 19 an ExecutorService is AutoCloseable, so try-with-resources shuts it down and waits.

Running several tasks

java
try (var pool = Executors.newFixedThreadPool(3)) {
    List<Callable<String>> jobs = List.of(() -> "a", () -> "b", () -> "c");
    List<Future<String>> results = pool.invokeAll(jobs);
    for (Future<String> f : results) {
        System.out.println(f.get());
    }
}

invokeAll runs every job and returns the futures in the order of the list, so this prints a, b, c in that order even though the jobs run in parallel. Output from the tasks themselves would interleave unpredictably.

Composing with CompletableFuture

java
CompletableFuture<String> greeting = CompletableFuture
        .supplyAsync(() -> "hello")
        .thenApply(String::toUpperCase)
        .thenCombine(CompletableFuture.supplyAsync(() -> "world"), (a, b) -> a + " " + b);

System.out.println(greeting.join());     // HELLO world

supplyAsync runs on the common pool unless you pass an executor. For blocking calls, pass your own executor, so you do not starve the shared pool.

The rule under everything

Threads run at the same time and share memory. Two threads incrementing a plain int lose updates, and a value written by one may never be seen by another. The fixes are locks (synchronized), volatile for visibility and atomics for counters. Which tool for which problem is in volatile vs synchronized vs atomic, and the formal guarantees in the memory model. The simplest safe design shares nothing mutable: pass immutable messages.

Virtual threads

Java 21 finalized virtual threads (JEP 444). A virtual thread is cheap, so you create one per task:

java
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    for (int i = 0; i < 10_000; i++) {
        executor.submit(() -> fetch());
    }
}

They shine when tasks mostly wait for I/O. For heavy computation, use a fixed pool near the core count, because more threads than cores only add switching.

Shut a pool down properly

A pool created with Executors.newFixedThreadPool keeps its threads alive, and the JVM does not exit while they exist. A pool that is never shut down is a leak in a test suite and a hang in a command-line tool. The pattern has two steps, a polite request and a forced stop after a deadline:

java
void shutdown(ExecutorService pool) {
    pool.shutdown();                                  // no new tasks, finish queued ones
    try {
        if (!pool.awaitTermination(30, TimeUnit.SECONDS)) {
            pool.shutdownNow();                       // interrupt running tasks
            if (!pool.awaitTermination(10, TimeUnit.SECONDS)) {
                System.err.println("Pool did not terminate");
            }
        }
    } catch (InterruptedException e) {
        pool.shutdownNow();
        Thread.currentThread().interrupt();
    }
}

Since Java 19, ExecutorService is AutoCloseable, and close() does the polite part and waits, so a try-with-resources block replaces most of the above for short-lived pools. Tasks must respond to interruption for shutdownNow to work: a loop that never checks Thread.currentThread().isInterrupted() and never calls a blocking method keeps running.

Exceptions vanish inside submit

An exception thrown in a task passed to execute goes to the thread's uncaught exception handler and prints a stack trace. An exception in a task passed to submit is stored in the returned Future, and nothing is printed until someone calls get:

java
Future<?> f = pool.submit(() -> {
    throw new IllegalStateException("boom");
});
// nothing happens here, the failure is silent
f.get();    // throws ExecutionException wrapping the IllegalStateException

A fire-and-forget submit that fails is the commonest way to lose an error in concurrent code. Either keep the future and check it, or catch and log inside the task. A wrapper that logs every failure belongs in one helper used by all background work.

Sizing a pool from the work

The right size depends on what the tasks do while they run:

Kind of taskPool sizeReason
CPU-bound, no waitingNumber of cores, or one moreMore threads only add switching
I/O-bound, mostly waitingCores times (1 plus wait time over compute time)Threads spend time blocked, so more can run
MixedSeparate pools for each kindOne slow call must not starve the computation
Unbounded, short tasksVirtual threadsNo pool size to tune

A task that waits 90 ms on a database for each 10 ms of computation has a ratio of 9, so a pool for 8 cores is about 80 threads, and it cannot exceed the number of database connections available. The formula gives a start, and a load test finds the real number.

Name your threads

A thread dump full of pool-3-thread-7 says nothing about who owns it. A ThreadFactory gives threads a name that points to the code:

java
ThreadFactory factory = Thread.ofPlatform().name("invoice-worker-", 0).daemon(false).factory();
ExecutorService pool = Executors.newFixedThreadPool(4, factory);

Thread.ofPlatform().name(prefix, start) numbers the threads invoice-worker-0 and so on. When a dump later shows eight threads stuck in the same method, the prefix tells which component to look in. Naming costs one line and saves an hour in production.

Waiting for several results

invokeAll runs a list of tasks and returns when all are done, and invokeAny returns the first successful result and cancels the others:

java
List<Callable<Price>> quotes = List.of(() -> vendorA.quote(item), () -> vendorB.quote(item));
Price cheapest = pool.invokeAny(quotes);

invokeAny suits a request to redundant providers where any answer is good. If every task fails, it throws an ExecutionException with the last failure. Give both a timeout overload, so one hung vendor does not hold the caller forever.

Reading a thread dump for the first time

A thread dump is the best way to learn how threads behave in a real program. Start a small server, take a dump with jcmd <pid> Thread.print, and read the states: RUNNABLE for running or ready, BLOCKED for waiting on a monitor, WAITING and TIMED_WAITING for parked or sleeping. Find your pool's thread names, and compare the stacks of idle and busy workers.

Doing it once on a harmless program makes the dump familiar, so that on the day of an incident the output looks routine and not frightening.

Where threads are the wrong abstraction

Many tasks are better as a parallel stream (list.parallelStream()) when the work is a pure computation over a collection, or as a message queue between components. For deadlocks and rejected tasks, see deadlocks and RejectedExecutionException. The java.util.concurrent documentation is the reference.

Also inJava Core
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.