Concurrency Design Patterns: Messages, Worker Threads, Futures, Cancellation and Pipelines

How to introduce threads into an application: oneway messages, thread-per-message, worker threads and pools, completion callbacks, futures and joins, cooperative cancellation, push-flow pipelines, parallel decomposition with fork/join and barriers, and readers-writers — mapped from Doug Lea's patterns to modern Java 21 APIs.

On this page

Simple definition#

Once objects are safe to share (see Designing concurrent objects), the next question is how work gets onto threads and how results and failures come back. Doug Lea’s Concurrent Programming in Java catalogues the recurring answers. Most now have direct support in java.util.concurrent, so this page names each pattern, explains the forces behind it, and shows the modern API.

PatternProblem it solvesModern Java
Oneway messageCaller does not need to wait for the workexecutor.execute(task)
Thread-per-messageEach request handled concurrentlyExecutors.newVirtualThreadPerTaskExecutor()
Worker threadsBound the number of threads; reuse themThreadPoolExecutor
Completion callbackBe told when work finishesCompletableFuture.whenComplete
FutureStart work now, collect the result laterFuture, CompletableFuture
CancellationStop work that is no longer neededFuture.cancel(true), interruption, StructuredTaskScope
Push-flow pipelineStages connected by queuesBlockingQueue stages, Flow, Reactor
Parallel decompositionSplit a big job across coresForkJoinPool, parallel streams, CyclicBarrier
Readers-writersMany readers, occasional writersReentrantReadWriteLock, StampedLock

Oneway messages#

A oneway message is a request whose caller does not wait for a result: logging an event, sending a notification, updating a cache. Turning it into a task object and handing it to another thread is the Command pattern.

Design questions Lea raises for oneway messages:

  • Ordering: must messages be processed in the order sent? (Use a single-threaded executor or a per-key queue.)
  • Failure: who learns that it failed? (Nobody, unless you add a callback or log.)
  • Flow control: what if messages arrive faster than they are processed? (Bounded queues and a rejection policy.)

Thread-per-message#

Start a new thread for each request. Simple, and concurrency matches demand, but with platform threads each one costs about 1 MB of stack and an OS thread, so unbounded load exhausts memory.

Virtual threads (Java 21) make thread-per-message practical again for I/O-bound work: they are cheap enough to create one per request.

try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
    for (Request r : requests) {
        executor.submit(() -> handle(r));          // one virtual thread per message
    }
}                                                  // close() waits for all tasks

You still need to bound downstream resources (database connections, rate-limited APIs) with semaphores or pools. See Virtual Threads and Structured Concurrency.

Worker threads and thread pools#

A fixed set of worker threads takes tasks from a channel (a queue). Threads are reused, and the queue absorbs bursts. This is ThreadPoolExecutor:

ThreadPoolExecutor pool = new ThreadPoolExecutor(
        8, 16,                                   // core and maximum threads
        30, TimeUnit.SECONDS,                    // idle time before extra threads die
        new ArrayBlockingQueue<>(500),           // bounded channel
        Thread.ofPlatform().name("report-", 0).factory(),
        new ThreadPoolExecutor.CallerRunsPolicy());   // back-pressure when full
ChoiceOptions and trade-offs
ChannelBounded queue (back-pressure) vs unbounded (newFixedThreadPool: memory risk) vs SynchronousQueue (direct hand-off)
Saturation policyAbort (exception), caller-runs (slows the producer), discard, discard-oldest
Pool sizeCPU-bound ≈ number of cores; I/O-bound larger, or use virtual threads
IsolationSeparate pools per dependency (bulkheads) so one slow dependency cannot take all workers

The event loop (Swing’s event dispatch thread, Netty’s I/O threads) is a worker-thread design with exactly one worker, which also guarantees ordering and confinement.

Completion callbacks#

Instead of blocking for a result, the caller passes code to run when the work completes — the Observer pattern applied to one task.

CompletableFuture
        .supplyAsync(() -> pricing.quote(cart), pool)
        .thenApply(Quote::withTax)
        .whenComplete((quote, error) -> {
            if (error != null) log.warn("quote failed", error);
            else ui.show(quote);
        });

Watch which thread runs the callback (the completing thread or the caller, depending on timing; use the *Async variants with an executor to control it), and make sure failures reach someone.

Futures and joins#

A future represents a result that will exist later. Start several independent pieces of work, then collect them:

CompletableFuture<Customer> customer = CompletableFuture.supplyAsync(() -> crm.find(id), pool);
CompletableFuture<List<Order>> orders = CompletableFuture.supplyAsync(() -> orderRepo.byCustomer(id), pool);

Dashboard dashboard = customer.thenCombine(orders, Dashboard::new)
        .orTimeout(2, TimeUnit.SECONDS)
        .join();

Thread.join() is the primitive form: wait for a thread to finish. With Java 21+ preview structured concurrency, subtasks forked in a scope are joined, and failures and cancellation propagate, as a unit. See Virtual Threads and Structured Concurrency.

Cancellation#

Work is cancelled when a user gives up, a timeout fires, or a sibling task fails. Lea’s key point: cancellation must be cooperative. Java has no safe way to kill a thread (Thread.stop is removed); instead, a thread is asked to stop through interruption, and the task must check and respond.

public void run() {
    try {
        while (!Thread.currentThread().isInterrupted()) {
            Batch b = queue.take();             // blocking calls throw InterruptedException
            process(b);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();     // preserve the status for the owner
    } finally {
        releaseResources();                     // leave shared state consistent
    }
}
  • Future.cancel(true) interrupts the running task; ExecutorService.shutdownNow() interrupts all.
  • Long CPU loops must check isInterrupted() periodically.
  • Blocking socket I/O is not always interruptible; close the socket, or use timeouts.
  • Multiphase cancellation: ask politely (interrupt), wait a bounded time, then escalate (close resources, abandon the thread), as shutdown() → awaitTermination() → shutdownNow() does.

Push-flow pipelines#

A flow system connects stages with channels: each stage takes input, transforms it, and pushes output to the next stage. Each stage can run on its own thread, and the queues between them smooth out speed differences.

A push-flow pipeline with bounded queues between stages
flowchart LR
    S[Source<br/>read files] -->|BlockingQueue| P[Parse]
    P -->|BlockingQueue| E[Enrich<br/>2 workers]
    E -->|BlockingQueue| W[Write<br/>batch to DB]

Lea’s guidance for flow systems:

  • Exclusive ownership: a message belongs to one stage at a time. After a stage passes an object on, it must not touch it again. Ownership transfer removes the need to lock the message itself.
  • Bounded channels give back-pressure: a slow writer makes the queues fill up and slows the source.
  • Stage shapes: linear stages, joiners (two inputs), splitters (two outputs), and their configuration should be separate from the stage logic.

Modern equivalents: java.util.concurrent.Flow (Reactive Streams with explicit demand), Project Reactor, Kafka Streams, and at system scale, the stream processing architectures.

Parallel decomposition#

To make one large computation faster, split it across cores:

  • Fork/join: recursively split a task until pieces are small, compute them in parallel, and combine results. ForkJoinPool uses work stealing to keep all cores busy. Parallel streams are built on it.
  • Barriers: in iterative algorithms (simulations, matrix computations), each worker computes its part of a step, then all wait at a CyclicBarrier before the next step.
long total = LongStream.rangeClosed(1, 50_000_000)
        .parallel()                                // fork/join under the hood
        .filter(n -> n % 3 == 0)
        .sum();

Parallelism helps only for CPU-bound work on large inputs with independent elements. For small inputs or blocking I/O, it is slower than a simple loop.

Readers and writers#

When reads vastly outnumber writes and reads take long enough to matter, let many readers in at once but give writers exclusive access. Lea implements the policy with before/after methods (a Template Method around the read or write) and discusses the policy choices: do waiting writers block new readers (writer preference, avoids writer starvation) or not?

private final ReentrantReadWriteLock rw = new ReentrantReadWriteLock();

public Price lookup(String sku) {
    rw.readLock().lock();
    try { return prices.get(sku); } finally { rw.readLock().unlock(); }
}

public void reload(Map<String, Price> fresh) {
    rw.writeLock().lock();
    try { prices.clear(); prices.putAll(fresh); } finally { rw.writeLock().unlock(); }
}

Often simpler: keep the data in an immutable map and replace the reference atomically (volatile field or AtomicReference). Readers then need no lock at all. StampedLock adds optimistic reads for very hot read paths.

Choosing a design#

Choosing how to introduce concurrency
flowchart TD
    A[Work to run concurrently] --> B{Caller needs<br/>the result?}
    B -- no --> C[Oneway message:<br/>executor.execute]
    B -- yes --> D{Blocking I/O<br/>per request?}
    D -- yes --> E[Thread-per-message<br/>with virtual threads<br/>+ futures / structured scope]
    D -- no --> F{One big CPU-bound job?}
    F -- yes --> G[Parallel decomposition:<br/>fork/join, parallel streams]
    F -- no --> H{Stream of items through<br/>several stages?}
    H -- yes --> I[Pipeline with bounded queues<br/>or reactive streams]
    H -- no --> J[Worker pool with<br/>bounded queue]

Common mistakes#

  • Losing exceptions in execute()d tasks or futures nobody reads.
  • Blocking inside a ForkJoinPool (including the common pool used by parallel streams and default CompletableFuture async methods).
  • Ignoring interruption, so shutdown hangs.
  • Sharing a message after handing it to the next pipeline stage.
  • One pool for everything, so a slow dependency starves unrelated work.

Quick check#

What is the difference between thread-per-message and worker threads? Thread-per-message creates a thread for every request; worker threads reuse a bounded set of threads that take tasks from a queue.

Why is cancellation in Java cooperative? Threads cannot be safely killed; they are interrupted and must check the flag or handle InterruptedException to stop cleanly.

What does exclusive ownership mean in a pipeline? A message is owned by one stage at a time; after passing it on, the sender no longer touches it, so the message needs no locking.

When does a parallel stream help? For large, CPU-bound computations over independent elements; not for small inputs or blocking I/O.

What is a simpler alternative to a read-write lock for read-mostly data? An immutable snapshot replaced atomically through a volatile or AtomicReference field.

Summary#

  • Choose how work reaches threads: oneway messages, thread-per-message, or worker pools.
  • Get results with callbacks, futures or structured scopes; never lose failures.
  • Cancellation is cooperative: honour interruption and clean up.
  • Pipelines need ownership transfer and bounded channels; parallel decomposition suits CPU-bound work.

References#

  • Doug Lea. Concurrent Programming in Java: Design Principles and Patterns (2nd edition). Addison-Wesley, 1999. Chapter 4: Creating Threads (oneway messages, services in threads, parallel decomposition, active objects) and the concurrent application architectures (flow, parallel, layered).
  • Brian Goetz et al. Java Concurrency in Practice. Addison-Wesley, 2006. Chapters 6–8.
  • JEP 444: Virtual Threads (Java 21).