Java Programming
Java Concurrency: Threads, Synchronization, Executors and Virtual Threads
Advanced9 min readUpdated
Concurrent code fails in ways that never show up in a unit test, so the only defence is understanding the rules. This guide covers what a thread is, why two threads can disagree about a variable's value, how synchronized, volatile, locks and atomics establish visibility and atomicity, how executors and CompletableFuture structure work, and how Java 21 virtual threads change the calculus for I/O-heavy services. You will be able to write shared-state code that is correct, not just lucky.
What you will learn
- Explain the Java memory model concepts of atomicity, visibility and happens-before
- Protect shared state correctly with synchronized, volatile, java.util.concurrent locks and atomics
- Run and coordinate tasks with ExecutorService, Future and CompletableFuture
- Choose concurrent collections and blocking queues for producer-consumer designs
- Handle interruption and shutdown properly and know when virtual threads apply
Multithreading in Java is not hard because the API is large — most programs need a handful of classes — but because the failure modes are invisible: code that is wrong runs correctly on your laptop a million times and corrupts an order total on a 32-core server on a Friday night. The rules that decide correctness are the Java memory model's, and once you know them, the constructs in java.util.concurrent stop looking like a menu and start looking like answers to specific questions. This guide targets Java 21, which also introduced virtual threads.
Threads and what goes wrong
A thread is an independent path of execution sharing the process's heap with every other thread. Creating one is trivial:
Thread t = new Thread(() -> System.out.println("running on " + Thread.currentThread().getName()));
t.start(); // schedules the thread; run() would just call the method on the current thread
t.join(); // wait for it to finish
The trouble begins with shared mutable state. Two properties must hold for shared data to be correct:
- Atomicity — an operation completes entirely or not at all from the point of view of other threads.
count++is not atomic: it is a read, an add and a write, and two threads interleaving them lose an increment. - Visibility — a write by one thread is eventually seen by another. Without synchronisation there is no such guarantee: the JIT may keep a value in a register, reorder statements, or a core may serve a stale cache line. A loop
while (!stopped) {}can genuinely spin forever even after another thread setsstopped = true.
The Java memory model (JLS chapter 17) defines when a write is guaranteed visible to a read through the happens-before relation. The relevant edges: unlocking a monitor happens-before every subsequent lock of it; a write to a volatile field happens-before every subsequent read of it; Thread.start() happens-before anything in the started thread; everything in a thread happens-before join() on it returns; submitting a task to an executor happens-before the task runs. Every correct piece of concurrent code relies on one of these edges, even if the author never names it.
synchronized, volatile and atomics
synchronized acquires the intrinsic lock (monitor) of an object, runs the block, releases it. Only one thread holds a given monitor at a time, so the block is atomic with respect to other blocks on the same monitor, and the lock/unlock pair provides visibility.
public final class Counter {
private long value; // guarded by this
public synchronized void increment() { value++; } // locks `this`
public synchronized long get() { return value; } // reads need the lock too, for visibility
}
Note the synchronized on get(). A lock only protects data when every access, reads included, takes it. Synchronising writers alone still leaves readers with stale values.
A volatile field gives visibility without mutual exclusion: reads and writes go straight to main memory (conceptually) and are not reordered with surrounding code. It is the right tool for a flag or a reference that one thread publishes and others read; it is the wrong tool for count++.
private volatile boolean running = true;
public void stop() { running = false; } // visible to the worker immediately
public void run() {
while (running) { doWork(); } // exits promptly after stop()
}
For counters and single-variable updates, java.util.concurrent.atomic provides lock-free classes built on the CPU's compare-and-swap instruction: AtomicInteger, AtomicLong, AtomicReference, AtomicBoolean and, for high-contention counting, LongAdder. incrementAndGet, compareAndSet and updateAndGet(fn) are atomic and visible without a lock.
private final AtomicLong requests = new AtomicLong();
requests.incrementAndGet();
private final AtomicReference<Config> config = new AtomicReference<>(initial);
config.updateAndGet(c -> c.withTimeout(Duration.ofSeconds(5))); // retried until the CAS succeeds
The other route to safety is to have no shared mutable state: immutable objects (records, List.of) can be handed between threads freely, and the simplest concurrent design is often "each task owns its data; results are combined at the end".
Locks and conditions
synchronized cannot time out, cannot be interrupted while waiting and always releases in the block's scope. ReentrantLock offers tryLock(timeout), lockInterruptibly() and fairness, at the price of manual release in a finally:
private final ReentrantLock lock = new ReentrantLock();
public boolean transfer(Account to, long cents) throws InterruptedException {
if (!lock.tryLock(1, TimeUnit.SECONDS)) return false; // give up rather than deadlock
try {
debit(cents);
to.credit(cents);
return true;
} finally {
lock.unlock(); // always
}
}
ReentrantReadWriteLock lets many readers proceed while writers are exclusive, useful for read-mostly caches; StampedLock adds optimistic reads for the same case at a higher complexity cost.
Deadlock occurs when thread A holds lock 1 and waits for lock 2 while thread B holds 2 and waits for 1. The classic fix is a global lock ordering (always lock the account with the lower ID first); tryLock with a timeout is the fallback when ordering is impractical. A thread dump (jcmd <pid> Thread.print) shows deadlocked threads explicitly.
Object.wait()/notifyAll() and Condition.await()/signalAll() let a thread sleep until some state changes. Always call them in a loop that rechecks the condition, because spurious wake-ups are permitted and another thread may have consumed the state. In practice you rarely write this yourself; BlockingQueue, CountDownLatch, Semaphore and CompletableFuture encapsulate it.
Executors: the right way to run tasks
Creating a thread per task does not scale and gives you no lifecycle. An ExecutorService decouples what to run (a Runnable or Callable<T>) from how (a pool of N threads, a single thread, a virtual thread per task):
ExecutorService pool = Executors.newFixedThreadPool(8);
try {
List<Callable<Long>> jobs = files.stream()
.map(f -> (Callable<Long>) () -> Files.size(f))
.toList();
long total = 0;
for (Future<Long> f : pool.invokeAll(jobs)) {
total += f.get(); // rethrows the task's exception as ExecutionException
}
System.out.println("Total bytes: " + total);
} finally {
pool.shutdown(); // stop accepting; finish queued tasks
if (!pool.awaitTermination(30, TimeUnit.SECONDS)) pool.shutdownNow();
}
Since Java 19, ExecutorService implements AutoCloseable, and close() performs shutdown() followed by an unbounded awaitTermination(), so try-with-resources works for short-lived executors.
Pool sizing: for CPU-bound work, about the number of cores (Runtime.getRuntime().availableProcessors()); for blocking I/O with platform threads, more — or, better, virtual threads. ThreadPoolExecutor exposes the core size, maximum size, queue and rejection policy; the Executors factories hide these, and newCachedThreadPool in particular will create threads without bound under load. A ScheduledExecutorService runs tasks after a delay or on a fixed rate; it replaces java.util.Timer, whose single thread dies on the first uncaught exception.
Exceptions in tasks do not reach your logs unless you look: a Runnable submitted with execute goes to the thread's uncaught-exception handler, one submitted with submit is captured in the Future and silently lost if you never call get(). Wrap task bodies in a try/catch that logs, or always consume the future.
CompletableFuture: composing asynchronous work
Future.get() blocks. CompletableFuture lets you describe what happens when a result arrives, and combine several results, without blocking a thread until the very end:
CompletableFuture<Customer> customer = CompletableFuture.supplyAsync(() -> customers.load(id), pool);
CompletableFuture<List<Order>> orders = CompletableFuture.supplyAsync(() -> orderRepo.forCustomer(id), pool);
CompletableFuture<Invoice> invoice = customer
.thenCombine(orders, (c, os) -> Invoice.of(c, os)) // when both complete
.thenApply(pricing::apply) // transform
.exceptionally(ex -> Invoice.failed(id, ex)) // recover
.orTimeout(5, TimeUnit.SECONDS); // fail if too slow (Java 9)
Invoice result = invoice.join(); // block only at the edge
thenApply transforms, thenCompose chains a step that itself returns a future (the flatMap of futures), thenCombine joins two, allOf/anyOf join many, and handle/whenComplete/exceptionally deal with failures. Methods with the Async suffix run the continuation on a supplied executor rather than on whichever thread completed the previous stage; without a supplied executor, *Async methods and supplyAsync use the common ForkJoinPool, which is sized for CPU work and shared with parallel streams — never block in it.
Concurrent collections and queues
The collections in java.util.concurrent are designed for sharing, unlike the Collections.synchronizedX wrappers, which serialise every call and still leave iteration and check-then-act sequences unsafe.
| Class | Use |
|---|---|
ConcurrentHashMap | Shared map; compute, merge, putIfAbsent are atomic per key; iteration never throws |
CopyOnWriteArrayList | Listeners and configuration lists: rare writes, frequent reads, snapshot iteration |
ConcurrentLinkedQueue / ConcurrentLinkedDeque | Non-blocking queue for hand-off without waiting |
ArrayBlockingQueue / LinkedBlockingQueue | Producer–consumer with back-pressure: put blocks when full, take blocks when empty |
ConcurrentSkipListMap / Set | Sorted concurrent map/set |
ConcurrentHashMap.computeIfAbsent(key, fn) is atomic for the key, which makes a memoising cache one line — but fn must be short and must not touch the same map, or it deadlocks or throws IllegalStateException.
The producer–consumer pattern with a bounded BlockingQueue is the standard way to connect stages with different speeds; the bound is what stops a fast producer from exhausting memory. A poison pill element or shutdownNow signals consumers to stop.
Coordination utilities
CountDownLatch— wait until N events have happened (N workers started, N results arrived). One-shot.CyclicBarrier— N threads wait for each other at a point, then continue; reusable.Semaphore— limit concurrent access to a resource to K permits, for instance K simultaneous downloads.Phaser— a more general, multi-phase barrier.ThreadLocal— a per-thread variable, used for request context and non-thread-safe helpers such asSimpleDateFormat; alwaysremove()it at the end of the task when threads are pooled, or the value leaks to the next task.
Interruption and shutdown
Java has no safe way to kill a thread. Interruption is a cooperative request: thread.interrupt() sets a flag; blocking methods (sleep, wait, join, BlockingQueue.take, Future.get) respond by throwing InterruptedException and clearing the flag; CPU-bound loops must poll Thread.currentThread().isInterrupted() themselves.
The rule when you catch InterruptedException and cannot propagate it: restore the flag and stop as soon as you can.
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
Job job = queue.take(); // throws if interrupted while waiting
process(job);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // preserve the request for callers
}
}
Swallowing the exception is how services ignore shutdownNow() and keep running until the operating system kills them.
Virtual threads (Java 21)
A platform thread wraps an operating-system thread: about a megabyte of stack reserved, a few thousand per process at most, expensive to create. That is why thread pools exist, and why servers that block a thread per request cap out at a few thousand concurrent requests. A virtual thread (JEP 444, final in Java 21) is a lightweight thread scheduled by the JVM onto a small pool of carrier threads. When it blocks on I/O, its stack is parked on the heap and the carrier runs another virtual thread. Millions can exist at once.
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<String>> results = new ArrayList<>();
for (String url : urls) {
results.add(executor.submit(() -> httpClient.send(request(url), BodyHandlers.ofString()).body()));
}
for (Future<String> f : results) System.out.println(f.get().length());
} // close() waits for every task
The programming model is the familiar blocking one — no callbacks, no reactive operators — which is the point. Guidelines:
- Do not pool virtual threads. They are cheap; create one per task. Use a
Semaphoreto limit concurrency against a scarce resource instead. - Avoid
synchronizedaround blocking calls. On Java 21 a virtual thread inside asynchronizedblock or native call is pinned to its carrier while blocked, which defeats the purpose. Replace withReentrantLock, which does not pin. (Java 24 removed most pinning, but 21 is the LTS you are likely running.) - Be careful with
ThreadLocal. A million threads times a cached object each is a lot of memory; scoped values (a preview feature in Java 21) are the intended successor. - CPU-bound work gains nothing. Virtual threads reduce the cost of waiting, not of computing.
Spring Boot 3.2+ runs Tomcat request handling on virtual threads when spring.threads.virtual.enabled=true is set, which is the quickest way to try them in an existing service.
Common mistakes
Unsynchronised reads. Locking writers but reading a field without the lock, or without volatile, allows stale values indefinitely.
Check-then-act on a shared collection. if (!map.containsKey(k)) map.put(k, v) is a race; use putIfAbsent or computeIfAbsent.
Blocking in the common pool. CompletableFuture.supplyAsync(() -> jdbcCall()) without an executor, or blocking I/O in a parallel stream, starves every other user of ForkJoinPool.commonPool().
Losing task exceptions. submit() without get() hides the failure. Consume the future or log inside the task.
Unbounded queues and pools. newCachedThreadPool and LinkedBlockingQueue without a capacity turn a traffic spike into an OutOfMemoryError.
Forgetting shutdown(). Non-daemon pool threads keep the JVM alive after main returns.
Swallowing InterruptedException. Breaks cancellation for every caller above you.
Double-checked locking without volatile. The classic lazy-singleton bug; the holder-class idiom or a plain synchronized is simpler and correct.
ThreadLocal without remove(). Pooled threads carry the previous request's context into the next one — a security bug as well as a leak.
Key takeaways
- Correctness depends on the memory model: shared mutable data needs a happens-before edge — a lock,
volatile, an atomic, or a hand-off through an executor or concurrent collection — for both writers and readers. synchronizedgives atomicity and visibility;volatilegives visibility only; atomics give lock-free single-variable updates;ReentrantLockadds timeouts and interruptibility.- Run tasks through an
ExecutorServicewith a bounded pool and an explicit shutdown; compose asynchronous results withCompletableFutureand supply your own executor for blocking work. - Prefer
ConcurrentHashMap,CopyOnWriteArrayListand boundedBlockingQueues to synchronised wrappers; their per-key atomic operations remove most check-then-act races. - Interruption is cooperative: handle
InterruptedExceptionby restoring the flag and exiting. - Virtual threads make blocking thread-per-task code scale for I/O-bound services; create them per task, avoid
synchronizedaround blocking calls on Java 21, and expect no gain for CPU-bound work.
Sources and further reading
Frequently asked questions
- What is the difference between synchronized and volatile?
- volatile guarantees visibility: a write is seen by every subsequent read on any thread. It does not make compound operations such as count++ atomic. synchronized guarantees both mutual exclusion and visibility for everything done inside the block. Use volatile for a flag written by one thread and read by others; use synchronized, a Lock or an atomic class when a read-modify-write must be indivisible.
- When should I use virtual threads?
- For workloads with many concurrent tasks that spend most of their time blocked on I/O — HTTP handlers, database calls, message consumers. Virtual threads make thread-per-request scale to hundreds of thousands of tasks without pooling. They do not speed up CPU-bound work, and code holding a synchronized block across a blocking call pins the carrier thread on Java 21, so review those before switching.
- Why should I not create threads with new Thread() in application code?
- Unbounded thread creation exhausts memory and CPU under load, and raw threads give you no queueing, no result handling and no shutdown. An ExecutorService bounds concurrency, returns Futures, propagates exceptions and shuts down cleanly. For virtual threads, use Executors.newVirtualThreadPerTaskExecutor.
- How do I stop a thread safely?
- Ask it to stop by interrupting it, and write the task so that it checks Thread.interrupted() in loops and lets InterruptedException propagate from blocking calls. Thread.stop has thrown UnsupportedOperationException since Java 20 because it left locks and data in unknown states. Executors expose this through shutdownNow, which interrupts every running task.