Skip to content
Projetos

alxkm/java-concurrency-patterns

Ver no GitHub

Sobre

Concurrency patterns with benchmarks behind the claims and jcstress behind the memory model

Aprendizado e educação

Como estes números são medidos

O que você pode fazer

  • VirtualThreadsExample.java: Lightweight threads (Project Loom) for massive concurrency with minimal overhead.
  • StructuredConcurrencyExample.java: Treats a group of tasks as one unit of work: the first failure cancels the rest, a deadline bounds the whole group, and no task outlives the block. Built without the Java 21 preview API, and on threads rather than an executor, for a reason the class explains.

Crescimento medido de estrelas

em 24 h
0
em 7 dias
0
em 15 dias
+2
em 1 mês
Coletando histórico

Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.

README.md

Original

Java Concurrency Patterns

Concurrency examples that are measured, not asserted. Every performance claim here has a benchmark behind it, every antipattern has a test that catches it failing, and the memory model section proves its point with jcstress instead of prose.

Java CI with Gradle License: MIT Java

Here is why volatile exists, in something you can run in the next thirty seconds:

$ ./gradlew runExample -Pexample=VisibilityExample
plain field    -> reader observed the write: false     <- still spinning, forever
volatile field -> reader observed the write: true

The reader spins on a flag another thread sets. With a plain field it never stops: nothing in the loop writes the flag, so the JIT may hoist the read out and turn while (!flag) into if (!flag) while (true). That is legal precisely because no happens-before edge exists between the writer's store and the reader's load. One keyword creates the edge that forbids it.

What is different here

The memory model section proves things instead of stating them. The textbook Dekker probe for instruction reordering found zero in 20,000 hand-written attempts, and zero again in 500,000 barrier-synchronised iterations. jcstress found 9,914,377 in a single run, 3.74% of samples. Both numbers are here, and so is the reason the first one is zero.

Performance claims come with benchmarks. LongAdder at 1256 ops/us against 18 for synchronized under contention. Two claims this README used to make turned out to be wrong once measured, and they now carry the numbers that disproved them. See Benchmarks.

There is a section on debugging concurrency, not only on writing it. How to read a thread dump, why ReentrantLock never appears as BLOCKED, and virtual thread pinning costing 6x on Java 21 along with the two ways to see it. See Diagnostics.

Start here

You are Go to
new to this Memory model, then Patterns
debugging something right now Diagnostics
choosing between two options Benchmarks
looking for a specific class Contents below

Contents: 75 pattern examples across 31 topics, 44 antipattern examples across 15 topics, and JUnit 5 tests that assert the concurrency property in question rather than sleeping and hoping.

Contents

Getting started

Requires a JDK 21 or newer. The Gradle wrapper is checked in, so nothing else needs installing.

git clone https://github.com/alxkm/java-concurrency-patterns.git
cd java-concurrency-patterns

./gradlew build          # compile everything and run the test suite
./gradlew test           # run the tests on their own

Most examples carry a main method and are meant to be read and run one at a time. Run one from your IDE, or from the command line:

./gradlew listExamples                            # all 93 of them
./gradlew runExample -Pexample=AccountExample     # a simple name is enough
./gradlew runExample -Pexample=org.alxkm.antipatterns.racecondition.AccountExample

A few of the antipattern examples deliberately misbehave - DeadlockExample, for instance, is supposed to hang. That is the point; its resolution class next to it shows the way out.

Repository layout

src/main/java/org/alxkm/
├── patterns/        75 examples across 31 topics: atomics, locks, executors, queues,
│                    synchronizers, and the classic patterns (active object, balking,
│                    guarded suspension, monitor object, reactor, object pool, ...),
│                    plus Java 21 virtual threads and structured concurrency
└── antipatterns/    44 examples across 15 topics, each a broken version paired with its fix
                     (thread leakage, busy waiting, deadlock, lock contention, race conditions, ...)

src/test/java/org/alxkm/
├── patterns/        JUnit 5 tests for the pattern examples
├── antipatterns/    tests pinning down both the broken and the corrected behaviour
└── testsupport/     Await and Concurrently - helpers for writing tests that assert
                     concurrency properties deterministically, without Thread.sleep

Memory model

Every other section here shows a mechanism - a lock, a queue, an atomic. This one shows the rules those mechanisms exist to satisfy. Without them, "double-checked locking needs volatile" is a recipe to memorise rather than something you can reason about.

The model is defined in terms of happens-before: an ordering between actions in different threads. If a write happens-before a read, the read must see that write. If no such edge exists, the read may see the write, may see a stale value, or may see actions in a different order than the source lists them - and the compiler, JIT and CPU are all free to exploit that freedom.

Most of these have a diagrammed write-up in docs/diagrams: happens-before, visibility, reordering, false sharing.

Why volatile, concretely

Run the visibility example and the answer stops being abstract:

$ ./gradlew runExample -Pexample=VisibilityExample
plain field    -> reader observed the write: false
volatile field -> reader observed the write: true

The reader spins on a flag another thread sets. With a plain field it spins forever: nothing in the loop writes the flag, so the JIT may hoist the read out and turn while (!flag) into if (!flag) while (true). That is legal precisely because no happens-before edge exists between the writer's store and the reader's load. One volatile creates the edge and the loop exits.

Stress tests: what a unit test cannot show

Some of these races cannot be demonstrated by an ordinary test, and it is worth being precise about why. Lining two threads up requires synchronisation, and that synchronisation is itself a memory barrier that drains the store buffer producing the effect. Measured here, the textbook Dekker probe found zero reorderings in 20,000 thread-pair runs and zero in 500,000 barrier-synchronised iterations.

Why that happens is worth spelling out, because it is not bad luck:

flowchart LR
    A["Catch reordering<br/>in a unit test"] --> B["Two threads<br/>must line up"]
    B --> C["Lining up needs<br/>a barrier or latch"]
    C --> D["That IS a<br/>memory barrier"]
    D --> E["It drains the store buffer<br/>that produces the effect"]
    E --> F["The test destroys<br/>what it measures"]

jcstress, the OpenJDK harness built for this, spins the actors without synchronisation and shuffles JIT decisions between forks. Given the same idiom:

RESULT       SAMPLES     FREQ       EXPECT  DESCRIPTION
  0, 0     9,914,377    3.74%  Interesting  Reordering: neither load saw the other store
  0, 1   126,435,416   47.65%   Acceptable  actor1 ran first
  1, 0   128,998,594   48.61%   Acceptable  actor2 ran first

That gap - zero by hand, millions under jcstress - is the lesson. These bugs do not fail loudly in testing; they fail in production, rarely, on someone else's hardware.

The same suite marks the safe variants FORBIDDEN, so a run fails if a guarantee is ever violated: volatile fields must never produce 0, 0, and a final field must never be observed at its default.

Release/acquire sits between the two, and the suite shows both of its edges. A value handed over through a setRelease/getAcquire flag was never seen half done in about 400 million samples, where the same handover through plain fields was caught at it 116,180 times. Dekker's idiom through the same two modes still produced 0, 0 in 12.7% of samples: release/acquire does not order a store before a later load, and that ordering is the part of volatile you pay for.

The same harness settles an antipattern the unit tests can only describe. An unsynchronised lazy singleton is check-then-act, and two threads that both see null both construct. A unit test with 64 threads behind a start gate usually misses it. jcstress caught it in 258,119,096 of 4.09 billion samples, while the synchronized and double-checked versions never produced a second instance in over 2 billion samples each. See Lack of Thread Safety in Singletons.

./gradlew jcstress                                          # full run, a few minutes
./gradlew jcstress -PjcstressArgs="-t PlainFields -m quick"  # one test, faster

./gradlew build compiles these tests but does not run them - a full pass takes minutes, which does not belong in every build. Reports land in build/reports/jcstress.

Diagnostics

Knowing the primitives is not the same as being able to work out what a stuck process is doing at 3am. This section covers the other direction: given a system that has stopped making progress, how to find out why.

Taking a dump

jcmd <pid> Thread.print        # preferred
jstack <pid>                   # older, same idea

Take three, twenty seconds apart. One dump shows where threads are; three show whether they are moving. A thread in the same frame across all three is stuck, whereas one that moves is just busy.

Reading it

State Means Usually
RUNNABLE running, or wants to be also covers blocking socket reads, so not always "busy"
BLOCKED waiting to enter a synchronized block contention; the dump names the monitor and its owner
WAITING parked until someone signals a handoff; if nobody signals, it never returns
TIMED_WAITING parked with a deadline normal for pool workers and sleep
stateDiagram-v2
    direction LR
    [*] --> NEW
    NEW --> RUNNABLE: start()
    RUNNABLE --> BLOCKED: enters synchronized<br/>someone else holds it
    BLOCKED --> RUNNABLE: monitor released
    RUNNABLE --> WAITING: wait() / park()<br/>take() / await()
    WAITING --> RUNNABLE: notify / signal / unpark
    RUNNABLE --> TIMED_WAITING: sleep(n) / poll(n, unit)
    TIMED_WAITING --> RUNNABLE: signalled or timed out
    RUNNABLE --> TERMINATED: run() returns
    TERMINATED --> [*]

    note right of BLOCKED
        CONTENTION. Someone holds the monitor.
        You proceed when they release. Costs throughput.
    end note

    note right of WAITING
        A HANDOFF. Parked until signalled.
        If nobody signals, this is a HANG.
    end note

Two traps worth knowing:

BLOCKED and WAITING are different problems. BLOCKED is contention and resolves when the owner releases. WAITING is a handoff that may never come. One costs throughput, the other is a hang.

ReentrantLock never shows as BLOCKED. It parks the thread, so it appears as WAITING on an ownable synchronizer. Grepping a dump for BLOCKED misses every lock in java.util.concurrent. Run ThreadDumpExample to see all three shapes side by side.

Deadlocks

The JVM finds these itself. A thread dump ends with a Found one Java-level deadlock section, and the same analysis is available programmatically:

Found a Java-level deadlock involving 2 threads:

"holder-a" id=21 BLOCKED
    waiting to lock java.lang.Object@4d405ef7 which is held by "holder-b" id=22
    holds java.lang.Object@76fb509a

It covers ReentrantLock as well as monitors, but it only finds cycles. A thread blocked forever on a lock nobody will release is not a cycle and will not be reported, and neither will a livelock, where threads keep running without progressing. For those, three dumps and your own eyes.

Note that the detector is JVM-wide. Anything asserting on the result should scope it with deadlockedAmong(threads), or an unrelated cycle elsewhere in the process will fail the assertion.

Virtual thread pinning

A virtual thread that blocks normally unmounts from its carrier, which is what lets a few carriers serve thousands of threads. Blocking inside synchronized is the exception: on Java 21 the monitor is tied to the carrier, so the thread holds it for the whole block. Measured by VirtualThreadPinningExample, 64 tasks blocking 500ms each on 12 cores:

synchronized  : 3060 ms     12 at a time, so 6 rounds
ReentrantLock :  503 ms     all 64 at once

Every task locks a monitor of its own, so nothing there contends. What runs out is carriers.

Pinning is invisible in a thread dump; the symptom is throughput that will not scale. To see it:

java -Djdk.tracePinnedThreads=short ...   # prints the frame holding the monitor
Thread[#97,ForkJoinPool-1-worker-12,5,CarrierThreads]
    org.alxkm.diagnostics.VirtualThreadPinningExample.lambda$runPinned$0(...) <== monitors:1

The jdk.VirtualThreadPinned JFR event records the same thing with far less overhead, which makes it the option for a production process.

flowchart TD
    A["Throughput will not scale,<br/>and there is no lock contention"] --> B["Run with<br/>-Djdk.tracePinnedThreads=short"]
    B --> C["Prints the frame<br/>holding the monitor"]
    A --> D["In production, use the<br/>jdk.VirtualThreadPinned JFR event"]
    C --> F{"Is the blocking call<br/>inside synchronized?"}
    F -->|yes| G["Swap the monitor<br/>for a ReentrantLock"]
    F -->|no| H["Look elsewhere:<br/>native frames also pin"]

Longer write-ups: thread states and dumps, virtual thread pinning.

The fix is a ReentrantLock, which a virtual thread can hold across an unmount. Everywhere else synchronized is fine. This advice has an expiry date: JEP 491 removed monitor pinning in Java 24, so on a recent JDK both versions run in the same time. It still matters on 21, the current LTS and what this repository builds against.

Benchmarks

A repository that makes performance claims should be able to back them. These are JMH benchmarks for the specific claims made above, and the numbers below come from running them, not from an article.

./gradlew jmh                                              # everything, several minutes
./gradlew jmh -PjmhArgs="CounterBenchmark -t 8"            # one benchmark, contended
./gradlew jmh -PjmhArgs="CounterBenchmark -f 1 -wi 3 -i 3" # quick and rough

./gradlew build compiles them but never runs them. Results land in build/reports/jmh.

All figures below are throughput in ops/us, higher is better, on a 12 core machine running JDK 21. Your numbers will differ; the point is that you can produce your own.

Counters

CounterBenchmark.java

1 thread 8 threads
synchronized 92.9 18.2
ReentrantLock 96.1 66.6
AtomicLong 204.2 115.5
LongAdder 210.3 1256.5

Two things worth noting. Uncontended, the atomics are already about twice as fast as either lock, which contradicts the old advice that an uncontended monitor is nearly free. That advice assumed biased locking, disabled in JDK 15 and removed in 18.

Under contention the spread is much wider, and LongAdder is in a different class: 69x the throughput of synchronized. It gets there by spreading its state across padded cells so threads stop fighting over one cache line, which is the effect FalseSharingExample measures directly. The catch is that sum() has to walk every cell, so a counter read as often as it is written is a different question from this one.

Queues

Which of the seven is usually the question people arrive with:

flowchart TD
    A["I need a queue<br/>between threads"] --> B{"Need blocking,<br/>i.e. backpressure?"}
    B -->|no| C{"Need LIFO too?"}
    C -->|no| D["ConcurrentLinkedQueue<br/>4.4 ops/us"]
    C -->|yes| E["ConcurrentLinkedDeque<br/>3.2 ops/us, ~27% slower"]
    B -->|yes| F{"Special delivery<br/>order?"}
    F -->|by priority| G["PriorityBlockingQueue"]
    F -->|after a delay| H["DelayQueue"]
    F -->|producer must know<br/>it was picked up| I["LinkedTransferQueue"]
    F -->|plain FIFO| J["ArrayBlockingQueue<br/>23.6 ops/us"]

QueueBenchmark.java offers and polls from the same thread, 4 threads:

ops/us
ArrayBlockingQueue 23.6
LinkedBlockingQueue 11.1
ConcurrentLinkedQueue 4.4
ConcurrentLinkedDeque 3.2

ConcurrentLinkedDeque came out about 27% slower than ConcurrentLinkedQueue, which is the same direction as the 40% this README used to quote, but not the same number.

The LinkedBlockingQueue result contradicts what this README used to claim. Its two-lock design is supposed to beat ArrayBlockingQueue, but that argument is about producers and consumers running at once, which offer-then-poll on one thread cannot show either way. So QueueHandoffBenchmark.java runs 4 producers against 4 consumers:

total produce consume
ArrayBlockingQueue 49.5 ± 1.4 23.7 25.8
LinkedBlockingQueue 40.9 ± 18.2 15.2 25.7

Closer, as expected, but still not in favour of the two-lock design here. ArrayBlockingQueue writes into a ring buffer it allocated once; LinkedBlockingQueue allocates a node per element. Note the error bars: the linked queue's throughput is also far less predictable.

Lists

ListBenchmark.java, 1000 elements, throughput of the whole group:

7 readers, 1 writer 4 readers, 4 writers
CopyOnWriteArrayList 314.7 300.3
Collections.synchronizedList 19.3 12.4

The advice that CopyOnWrite suits infrequent writes is right, but it understates the case: the crossover is much further out than "infrequent" suggests. Even at an even read/write split it was 24x ahead here, because its reads take no lock at all (296 against 6.8 ops/us) and that dominates the total.

Read the write column separately before concluding too much. CopyOnWrite writes were 4.1 ops/us against 5.8 for the synchronized list at 1000 elements, and the gap widens with size, since every write copies the whole array. At 100,000 elements CopyOnWrite writes become both slower and very erratic. Total throughput still favoured CopyOnWrite at every size tested, which is a statement about this workload rather than a general rule.

The same choices as decision trees, with the counter and thread-type cases too: docs/diagrams/07-choosing.md.

Patterns

Atomics

  • AtomicExample.java: Demonstrates basic atomic operations.
  • AtomicMarkableReferenceExample.java: A lock-free sorted list, and why removing a node needs the mark: without it, an insert next to a remove is lost.
  • AtomicReferenceArrayExample.java: Booking seats with one compare-and-set per element: a volatile array confirms one seat to two people, and compareAndSet compares references, not equals().
  • AtomicReferenceExample.java: A range whose lower bound may never pass its upper: two AtomicIntegers let two setters break the rule together, one immutable pair in an AtomicReference does not.
  • AtomicStampedReferenceExample.java: The ABA problem on a lock-free stack, and how the stamp catches it.
  • FieldUpdaterExample.java: A reference-counted buffer on plain volatile fields, the way Netty counts references: the three field updaters, the rules newUpdater enforces, and why retain() needs a compare-and-set loop.
  • VarHandleGauge.java: Atomic updates on plain fields through a VarHandle, one by fetch-and-add and one by a compare-and-set loop.

Concurrent collections

Queue

Executors

Fork join pool

  • ForkJoinMergeSort.java: Example of merge sort using ForkJoinPool.
  • ForkJoinPoolExample.java: A RecursiveTask that sums an array: split by index, fork one half and compute the other, and an exception from any subtask reaching the caller.

Future

  • FutureExample.java: What get() and cancel() do and do not do: a timed-out get() leaves the task running, cancel() only interrupts, and a cancelled result is lost.

Locks

Synchronizers

Thread local

Reentrant lock

  • ReentrantLockCounter.java: Counter using ReentrantLock.
  • ReentrantLockExample.java: Opposite transfers between two accounts: lock() deadlocks and the JVM reports it, tryLock() with a random back-off does not. Also lockInterruptibly() against lock().

Mutex

Semaphore

Double check locking singleton

Active object

Balking pattern

Guarded suspension pattern

Immutable pattern

Monitor object pattern

Multithreaded context

Reactor pattern

Scheduler

Thread-safe lazy initialization

Two-phase termination

Thread-Safe Builder Pattern

Concurrent Object Pool Pattern

Leader-Follower Pattern

Modern Java features (Java 21+)

  • VirtualThreadsExample.java: Lightweight threads (Project Loom) for massive concurrency with minimal overhead.
  • StructuredConcurrencyExample.java: Treats a group of tasks as one unit of work: the first failure cancels the rest, a deadline bounds the whole group, and no task outlives the block. Built without the Java 21 preview API, and on threads rather than an executor, for a reason the class explains.

Odd-Even Printer

Philosopher problem

Producer-Consumer with BlockingQueue Variations

Antipatterns

Thread Leakage

  • Description: Threads are created but never terminated, leading to resource exhaustion.
  • Solution: Use thread pools (e.g., ThreadPoolExecutor) to manage threads.

Examples

Busy Waiting

  • Description: A thread repeatedly checks a condition in a loop, wasting CPU cycles.
  • Solution: Use wait/notify mechanisms or higher-level concurrency constructs like CountDownLatch, CyclicBarrier, or Condition.

Examples

Nested Monitor Lockout (Deadlock)

  • Description: Two or more threads block each other by holding resources the other needs.
  • Solution: Always acquire multiple locks in a consistent global order, use tryLock with timeouts, or avoid acquiring multiple locks if possible.

Examples

Forgotten Synchronization

  • Description: Access to shared resources is not properly synchronized, leading to race conditions.
  • Solution: Use synchronized blocks or higher-level concurrency utilities (e.g., ReentrantLock, Atomic* classes).

Examples

Excessive Synchronization

  • Description: Overuse of synchronization, leading to contention and reduced parallelism.
  • Solution: Minimize the scope of synchronized blocks, use lock-free algorithms, or utilize concurrent collections (e.g., ConcurrentHashMap, CopyOnWriteArrayList).

Examples

Using Thread-Safe Collections Incorrectly

  • Description: Assuming individual thread-safe operations guarantee overall thread-safe logic.
  • Solution: Combine operations using explicit locks or use higher-level synchronization constructs to maintain logical thread safety.

Examples

Ignoring InterruptedException

  • Description: Swallowing or ignoring the InterruptedException, leading to threads that cannot be properly managed or interrupted.
  • Solution: Handle interruptions properly, typically by cleaning up and propagating the interruption status.

Examples

Starting a Thread in a Constructor

  • Description: Starting a thread from within a constructor, possibly before the object is fully constructed.
  • Solution: Start threads from a dedicated method called after construction, or use factory methods.

Examples

Double-Checked Locking

  • Description: A broken idiom for lazy initialization that was incorrectly implemented before Java 5.
  • Solution: Use the volatile keyword correctly or the Initialization-on-demand holder idiom.

Examples

Lock Contention

  • Description: Multiple threads trying to acquire the same lock, leading to reduced performance.
  • Solution: Reduce the granularity of locks, use read-write locks, or employ lock-free data structures.

Examples

Improper Use of ThreadLocal

  • Description: A ThreadLocal value set on a pooled thread and never removed outlives its task. The next task on that thread inherits it, so one request can see another request's user. It is also never collected while the thread lives.
  • Solution: Remove the value when the task ends, in a finally block or through a try-with-resources cleaner.

Examples

Non-Atomic Compound Actions

  • Description: Performing compound actions (e.g., check-then-act, read-modify-write) without proper synchronization.
  • Solution: Use atomic variables or synchronized blocks to ensure compound actions are atomic.

Examples

Race Conditions

  • Description: The system's behavior depends on the sequence or timing of uncontrollable events.
  • Solution: Properly synchronize access to shared resources and use thread-safe collections.

Examples

Lack of Thread Safety in Singletons

  • Description: Singleton instances not properly synchronized, leading to multiple instances.
  • Solution: Use the enum singleton pattern or the Initialization-on-demand holder idiom.

Examples

Stress test

  • LazySingletonStressTest.java - the unsynchronised getInstance() handed two threads two different instances in 6.32% of 4.09 billion samples. The synchronized and double-checked versions never did, in 2.46 and 2.21 billion.

Using Threads Instead of Tasks

  • Description: Directly creating and managing threads instead of using the Executor framework.
  • Solution: Use ExecutorService and related classes to manage thread pools and tasks efficiently.

Examples

java.util.concurrent.*

image

Concurrent Collections are a set of collections designed to operate more efficiently in multithreaded environments compared to the standard universal collections from the java.util package. Instead of using the basic Collections.synchronizedList wrapper, which blocks access to the entire collection, these collections utilize locks on data segments or employ wait-free algorithms to optimize parallel data reading and processing.

Queues - non-blocking and blocking queues with multithreading support. Non-blocking queues are designed for speed and work without blocking threads. Blocking queues are used when it is necessary to "slow down" the "Producer" or "Consumer" threads if some conditions are not met, for example, the queue is empty or full, or there is no free "Consumer".

Synchronizers are auxiliary utilities for synchronizing threads. They are a powerful weapon in "parallel" computing.

Executors - contains excellent frameworks for creating thread pools, scheduling asynchronous tasks and obtaining results.

Locks are alternative and more flexible thread synchronization mechanisms compared to the basic synchronized, wait, notify, notifyAll.

Atomics - classes with support for atomic operations on primitives and references.

Concurrent Collections

CopyOnWrite collections

image

The name is self-explanatory. All modification operations on the collection (add, set, remove) result in the creation of a new copy of the internal array. This ensures that when an iterator traverses the collection, a ConcurrentModificationException will not be thrown. It is important to note that only references to objects are copied during the array copy (shallow copy), meaning that access to the fields of elements is not thread-safe. CopyOnWrite collections are particularly useful when write operations are infrequent, such as when implementing a listener subscription mechanism and iterating through the listeners.

CopyOnWriteArrayList - A thread-safe analogue of ArrayList, implemented with the CopyOnWrite algorithm.

CopyOnWriteArraySet - Implementation of the Set interface, using CopyOnWriteArrayList as a basis. Unlike CopyOnWriteArrayList, there are no additional methods.

Examples

ConcurrentSkipListSet
CopyOnWriteArrayList

Scalable Maps

ConcurrentHashMap is the most repeated out-of-date fact in Java concurrency, and this README carried the old version of it until recently. Up to Java 7 the map was a fixed array of segments, each with its own lock, and concurrencyLevel set how many there were:

flowchart TD
    CHM["ConcurrentHashMap, Java 7"] --> S0["Segment 0<br/>own lock, own table"]
    CHM --> S1["Segment 1<br/>own lock, own table"]
    CHM --> S15["Segment 15<br/>own lock, own table"]
    WA["writer A"] --> S0
    WB["writer B"] --> S0
    S0 --> X["A holds the lock, B waits,<br/>even though their buckets differ.<br/>At most 16 writers, ever."]

Since Java 8 there are no segments at all. The lock granularity is the individual bin, so two writers collide only when their keys hash to the same bucket:

flowchart TD
    CHM["ConcurrentHashMap, Java 8+"] --> T["one table of bins"]
    T --> B0["empty bin"]
    T --> B1["bin: Node -> Node"]
    T --> BN["bin: TreeBin"]
    B0 --> C0["CAS the head in.<br/>No lock at all."]
    B1 --> C1["synchronized on<br/>THAT node only"]
    BN --> CN["over 8 entries becomes a<br/>red-black tree: O(log n),<br/>not O(n), on a bad hash"]

Concurrency now scales with table size instead of being capped at 16, and concurrencyLevel survives only as a sizing hint. Full write-up, including why size() is an estimate and what weakly consistent iterators really promise: docs/diagrams/09-concurrenthashmap.md.

Improved implementations of HashMap, TreeMap with better support for multithreading and scalability.

ConcurrentMap<K, V> - An interface that extends Map with several additional atomic operations.

ConcurrentHashMap<K, V> - Unlike Hashtable and synchronized blocks on HashMap, writes lock only the bin they touch rather than the whole map, so unrelated keys never contend. Up to Java 7 this was done with a fixed set of segments; since Java 8 the map locks the individual bin head and uses CAS for the common uncontended case, which is why concurrencyLevel is now only a sizing hint. Iterators are weakly consistent: they reflect the map at some point during traversal and never throw ConcurrentModificationException. See the ConcurrentHashMap javadoc for details.

Additional constructor

ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) - The third parameter is the anticipated number of concurrently updating threads, defaulting to 16. Since Java 8 it no longer selects a segment count; it is used only as a sizing hint for the initial table.

ConcurrentNavigableMap<K,V> - This interface extends the NavigableMap interface and mandates that objects implementing ConcurrentNavigableMap are used as return values. All iterators provided by this interface are designated as safe for use and are programmed not to throw ConcurrentModificationException.

ConcurrentSkipListMap<K, V> - This class serves as a thread-safe equivalent of TreeMap. It organizes data based on keys and ensures an average performance of log(N) for operations like containsKey, get, put, remove, and similar operations.

ConcurrentSkipListSet - This class implements the Set interface and is built upon ConcurrentSkipListMap for thread-safe set operations.

Examples

ConcurrentHashMap
ConcurrentSkipListMap

Queues

Non-Blocking Queues

image

Thread-safe and non-blocking queue implementations based on linked nodes.

ConcurrentLinkedQueue - This implementation uses the lock-free algorithm by Michael & Scott, built on CAS: a thread that is paused never holds up the others, and no call ever blocks. Every single call is thread-safe; a decision built from two calls is not. The usual consumer loop, while (!queue.isEmpty()) { int item = queue.poll(); }, has two bugs, and the example forces both. Two consumers can both pass isEmpty() for one item, and the one that loses gets null from poll(), which the unboxing turns into a NullPointerException; call poll() alone and check for null. And a consumer that finds the queue empty stops, although the producers may not have started yet: empty is not finished. The producers have to say they are done some other way, here with a count, and the consumer polls once more after it sees the count at zero, or an item offered at the last moment is left behind. A consumer that only waits should use a BlockingQueue and take() instead of spinning. size() walks the whole queue and is only an estimate while other threads change it.

ConcurrentLinkedDeque - Deque, pronounced as “Deck”, stands for Double-ended queue, indicating that data can be added to and removed from both ends. Consequently, this class supports both FIFO (First In First Out) and LIFO (Last In First Out) modes of operation. In practical scenarios, ConcurrentLinkedDeque should be employed only if LIFO functionality is indispensable, as its bidirectional nature costs throughput compared to ConcurrentLinkedQueue. Measured here it was about 27% slower on offer/poll; see Benchmarks.

Blocking Queues

image

BlockingQueue - When managing large data streams with queues, ConcurrentLinkedQueue alone may not suffice. If threads clearing the queue fail to keep up with the data influx, it could lead to memory exhaustion or significant IO/Net overload, causing a performance drop until system failure due to timeouts or lack of free descriptors. To address such scenarios, a queue with customizable size or conditional locking is necessary. This is where the BlockingQueue interface comes in, providing access to a range of useful classes. Besides setting the queue size, new methods have been introduced to handle underfilling or overflowing queues differently. For instance, when adding an element to a full queue, one method throws an IllegalStateException, another returns false, another blocks the thread until space is available, and yet another blocks the thread with a timeout, returning false if space is still unavailable. It's important to note that blocking queues don't support null values since null is used in the poll method as a timeout indicator.

ArrayBlockingQueue - A blocking queue implemented using a traditional ring buffer. In addition to the queue size, it allows control over lock fairness. If fair=false (default), thread order is not guaranteed. See the Locks section for more on "fairness".

DelayQueue - A specialized class that retrieves elements from the queue only after a delay specified in each element via the getDelay method of the Delayed interface.

LinkedBlockingQueue - A blocking queue implemented with linked nodes, using the "two lock queue" algorithm: one lock for adding, another for removing elements. The two locks let a put and a take proceed at once, which is often quoted as making it faster than ArrayBlockingQueue. Measured here that did not hold: ArrayBlockingQueue was ahead in both the single-threaded and the producer/consumer case, because it writes into a preallocated ring buffer while LinkedBlockingQueue allocates a node per element. It does consume more memory. See Benchmarks. The queue size is set via the constructor and defaults to Integer.MAX_VALUE.

PriorityBlockingQueue - A thread-safe wrapper over PriorityQueue. When inserting an element, its position in the queue is determined by the Comparator logic or the Comparable interface implemented in the elements. The smallest element is dequeued first.

SynchronousQueue - Operates on a "one in, one out" principle. Each insert operation blocks the producer thread until the consumer thread retrieves an element, and vice versa; the consumer waits until the producer inserts an element.

BlockingDeque - An interface providing additional methods for a bidirectional blocking queue, allowing data insertion and retrieval from both ends of the queue.

LinkedBlockingDeque - A bidirectional blocking queue implemented with linked nodes, essentially a doubly linked list with a single lock. The queue size is specified via the constructor and defaults to Integer.MAX_VALUE.

TransferQueue - This interface is interesting because it allows blocking the producer thread when adding an element until a consumer thread retrieves an element from the queue. The blocking can include a timeout or a check for waiting consumers, enabling synchronous and asynchronous message transfer mechanisms.

LinkedTransferQueue - An implementation of TransferQueue based on the Dual Queues with Slack algorithm, utilizing CAS and thread parking extensively when idle.

Examples

ArrayBlockingQueue example
ConcurrentLinkedDeque example
ConcurrentLinkedQueue example
BlockingQueue Producer-Consumer example
CustomBlockingQueue example

Synchronizers

image

This section introduces classes for active thread management:

Semaphore - Typically used to limit the number of threads accessing hardware resources or a file system. A counter controls access to a shared resource. If the counter is greater than zero, access is granted, and the counter is decremented. If the counter is zero, the current thread is blocked until another thread releases the resource. The number of permits and the "fairness" of thread release are specified via the constructor. The challenge with semaphores is setting the number of permits, often depending on hardware capabilities.

CountDownLatch - Allows one or more threads to wait until a specific number of operations in other threads are completed. For example, threads calling the latch's await method (with or without a timeout) will block until another thread completes initialization and calls the countDown method. This method decrements the count. When the counter reaches zero, all waiting threads proceed, and subsequent await calls pass without waiting. The count is one-time and cannot be reset.

CyclicBarrier - Used to synchronize a set number of threads at a common point. The barrier is reached when N threads call the await method and block. The counter then resets, and waiting threads are released. Optionally, a Runnable task can be executed before threads are unblocked and the counter is reset. In the example, each worker sums its share of a round and the barrier action adds up the shares, once per round, on the same barrier. The barrier also makes the shares visible to the action, and the action's result visible to every worker after await() returns.

The part that is easy to get wrong is failure. A worker that throws never reaches await(), and the others wait for it forever. It has to break the barrier on its way out, and reset() does not do that: it breaks the barrier only for the parties waiting at that moment, and a worker that arrives later waits on a fresh barrier for a party that will never come. An await() from an already interrupted thread breaks it for good, so every later arrival gets BrokenBarrierException. The test that runs a failing worker 300 times hangs within the first few runs if reset() is used instead.

Exchanger - Facilitates the exchange of objects between two threads, supporting null values for single object transfers or as a simple synchronizer. The first thread calling the exchange method blocks until the second thread calls the same method. The threads then exchange values and proceed.

Phaser - An advanced barrier for thread synchronization, combining features of CyclicBarrier and CountDownLatch. The number of threads is dynamic and can change. The class can be reused and allows threads to report readiness without blocking. In the example, workers do different numbers of steps, one per phase. arriveAndAwaitAdvance() keeps them in step, and a worker that has finished calls arriveAndDeregister() and leaves; the rest go on without it. A CyclicBarrier has a fixed number of parties and would wait for the leaver forever. Note that arrive() does not wait either: it is for a party that reports and moves on, not for one that needs the others to catch up.

Examples

Barrier wrapper
Barrier
CountDownLatch
Exchanger
Phaser
SemaphorePrintQueue

Executors

Here, we reach the most extensive section of the package. This part covers interfaces for executing asynchronous tasks with the capability of receiving results via the Future and Callable interfaces. Additionally, it includes services and factories for creating thread pools such as ThreadPoolExecutor, ScheduledThreadPoolExecutor, and ForkJoinPool. To enhance comprehension, we will break down the interfaces and classes into smaller, more manageable parts.

Future and Callable

image

Future - This is a useful interface for obtaining the results of an asynchronous operation. The key method is get, which blocks the current thread (with or without a timeout) until the asynchronous operation completes in another thread. Additional methods are available for canceling the operation and checking its current status. The FutureTask class often implements this interface. Three things about it are easy to get wrong. A get(timeout) that times out ends only the wait: the task keeps running and keeps its pool thread, so a caller that gives up on the result should cancel the task. cancel(true) only interrupts the thread running the task; a task that never checks for interruption runs to the end anyway. And once a future is cancelled, get() throws CancellationException even if the task went on to finish, so its result is lost. If the task threw, get() throws ExecutionException whose cause is the very exception the task threw.

RunnableFuture - While Future serves as a Client API interface, the RunnableFuture interface is used to start the asynchronous operation. The successful completion of the run() method marks the asynchronous operation as complete, allowing the results to be retrieved via the get method.

Callable - This is an extended version of the Runnable interface for asynchronous operations. It allows returning a typed value and throwing a checked exception. Although it lacks a run() method, many java.util.concurrent classes support it along with Runnable.

FutureTask - This class implements the Future and RunnableFuture interfaces. It accepts an asynchronous operation as input in the form of Runnable or Callable objects. The FutureTask class is designed to be launched in a worker thread, for example, via new Thread(task).start(), or through a ThreadPoolExecutor. The results of the asynchronous operation are retrieved using the get(...) method.

Delayed - This interface is used for asynchronous tasks that should start in the future, as well as in DelayQueue. It allows setting the time before the start of an asynchronous operation.

ScheduledFuture - A marker interface that combines the functionalities of Future and Delayed.

RunnableScheduledFuture - An interface that combines RunnableFuture and ScheduledFuture. It also allows specifying whether the task is one-time or should be launched at a specified frequency.

Executor Services

image

Executor - This is the fundamental interface for classes that execute Runnable tasks. It decouples the task submission process from the execution mechanism.

ExecutorService - An interface that defines a service for executing Runnable or Callable tasks. The submit methods take a task as a Callable or Runnable and return a Future through which the result can be obtained. The invokeAll methods handle lists of tasks, blocking the thread until all tasks in the provided list are completed or the specified timeout expires. The invokeAny methods block the calling thread until any one of the passed tasks completes. The interface also includes methods for graceful shutdown. Once the shutdown method is called, the service will no longer accept new tasks and will throw a RejectedExecutionException if an attempt is made to submit a task.

ScheduledExecutorService - This interface extends ExecutorService by adding capabilities for scheduling tasks to be executed after a delay or periodically.

AbstractExecutorService - An abstract class that serves as a base for building an ExecutorService. It provides the basic implementation of the submit, invokeAll, and invokeAny methods. Classes such as ThreadPoolExecutor, ScheduledThreadPoolExecutor, and ForkJoinPool inherit from this class.

Examples

Custom ExecutorService implementation
ExecutorCompletionService
Executors example
ExecutorServiceExample
ScheduledThreadPoolExecutors
ThreadPoolExecutors

ThreadPoolExecutor & Factory

image

ThreadPoolExecutor - A highly versatile and essential class used to execute asynchronous tasks within a thread pool. This approach minimizes the overhead associated with creating and terminating threads. By maintaining a fixed maximum number of threads in the pool, it ensures predictable application performance. It is generally recommended to create this pool using one of the factory methods provided by the Executors class. However, if the standard configurations are insufficient, all key parameters of the pool can be set via constructors or setters. For more details, refer to the relevant documentation.

ScheduledThreadPoolExecutor - In addition to the methods of ThreadPoolExecutor, this class allows tasks to be scheduled for execution after a specific delay or at a fixed rate, enabling the implementation of a timer service based on this class.

ThreadFactory - By default, ThreadPoolExecutor uses the standard thread factory obtained through Executors.defaultThreadFactory(). If additional customization is needed, such as setting thread priority or naming threads, you can implement this interface and pass it to ThreadPoolExecutor.

RejectedExecutionHandler - Defines a handler for tasks that cannot be executed by ThreadPoolExecutor for various reasons, such as a lack of available threads or the service being shut down. The ThreadPoolExecutor class includes several standard implementations: CallerRunsPolicy - runs the task in the calling thread; AbortPolicy - throws an exception; DiscardPolicy - silently discards the task; DiscardOldestPolicy - removes the oldest unexecuted task from the queue and retries adding the new task.

Fork Join

image

Java 1.7 introduces a new Fork Join framework for solving recursive problems using divide and conquer or Map Reduce algorithms.

Thus, by dividing into parts, it is possible to achieve their parallel processing in different threads. To solve this problem, you can use the usual ThreadPoolExecutor, but due to frequent context switching and tracking of execution control, all this does not work very effectively. Here, the Fork Join framework comes to our aid, which is based on the work-stealing algorithm. It reveals itself best in systems with a large number of processors. Doug Lea's design paper covers the algorithm and its performance characteristics in depth.

ForkJoinPool - The main entry point for initiating root (main) ForkJoinTask tasks. Subtasks are started using methods of the task being forked. By default, the thread pool is created with a number of threads equal to the number of processors (cores) available to the JVM.

ForkJoinTask - The base class for all Fork/Join tasks. Key methods include: fork() - adds a task to the queue of the current ForkJoinWorkerThread for asynchronous execution; invoke() - executes a task in the current thread; join() - waits for the subtask to complete and returns the result; invokeAll(…) - combines the previous three operations, executing two or more tasks at once; adapt(…) - creates a new ForkJoinTask from Runnable or Callable objects.

RecursiveTask - An abstract class derived from ForkJoinTask, requiring the implementation of the compute method, which performs the asynchronous operation. The usual shape is: below a threshold, compute directly; above it, split the range by index into [from, mid) and [mid, to), fork() the left half so an idle worker can steal it, compute() the right half in the current thread, then join() the left. Splitting by index matters: the earlier version of the example made two subtasks of size n / 2, and for odd n an element was dropped. An exception thrown in any subtask is rethrown by join() and invoke(), so the example sums with Math.addExact and an overflow anywhere in the tree reaches the caller instead of a wrong total.

RecursiveAction - Similar to RecursiveTask but does not return a result.

ForkJoinWorkerThread - Used as the default implementation in ForkJoinPool. Optionally, it can be extended to override worker thread initialization and completion methods.

Completion Service

image

CompletionService - An interface that separates the submission of asynchronous tasks from the retrieval of their results. The submit methods are used to add tasks, while the take method (blocking) and poll method (non-blocking) are used to obtain the results of completed tasks.

ExecutorCompletionService - A wrapper around any class that implements the Executor interface, such as ThreadPoolExecutor or ForkJoinPool. It is primarily used to abstract the task submission and execution monitoring process. If tasks are completed, their results can be retrieved; otherwise, the take method will wait for completion. The default service uses LinkedBlockingQueue, but any BlockingQueue implementation can be used.

Locks

image

Condition - An interface that provides alternative methods to the traditional wait/notify/notifyAll methods. A condition object is typically obtained from a lock using the lock.newCondition() method, allowing multiple wait/notify sets for a single object.

Lock - A fundamental interface in the lock framework that offers a more flexible approach to controlling access to resources or blocks compared to using synchronized. When using multiple locks, the release order can be arbitrary, and it provides an option to follow an alternative scenario if the lock is already held by another thread.

ReentrantLock - A reentrant lock that allows only one thread to enter a protected block at a time. This class supports both "fair" and "non-fair" thread locking. With "fair" locking, threads are released in the order they called lock(). With "unfair" locking, the release order is not guaranteed, but it operates faster. By default, "unfair" locking is used. What it adds over synchronized is the choice not to wait forever. tryLock() returns at once, and tryLock(timeout) after a while, so a thread that needs a second lock can let go of the first and retry instead of deadlocking; the example forces two opposite transfers to hold one account lock each and shows lock() deadlocking (the JVM's findDeadlockedThreads sees it) while the back-off version completes. The pause before retrying is random, or two threads can keep failing in step. lockInterruptibly() gives up when the waiting thread is interrupted; lock(), like synchronized, keeps waiting and sets the interrupt flag again only once it has the lock.

ReadWriteLock - An interface for creating read/write locks. These locks are particularly useful when the system has many read operations and few write operations.

ReentrantReadWriteLock - Commonly used in caches and other read-mostly structures. This class operates in two mutually exclusive modes: multiple readers can read data simultaneously, while only one writer can write data at a time. It pays only when reads are many, take a while and rarely overlap a write; each acquisition costs more than a plain lock, so with short reads ReentrantLock or synchronized is usually as fast. Two rules decide how to use it. There is no upgrade: a thread holding the read lock that calls writeLock().lock() waits for every reader to leave, itself included, so it waits forever. A downgrade is allowed: a writer may take the read lock and then let go of the write lock, so no other writer can change what it just wrote while it still uses it. The example is the cached-value pattern built on both. A reader that finds the value missing has to let go of the read lock before taking the write lock, and in that gap another thread may load it first, so the write path checks again. The tests also show that a new reader queues behind a waiting writer, even while the read lock is held, so a steady stream of readers cannot keep a writer out.

ReentrantReadWriteLock.ReadLock - A read lock for readers, obtained via readWriteLock.readLock().

ReentrantReadWriteLock.WriteLock - A write lock for writers, obtained via readWriteLock.writeLock().

LockSupport - Designed for creating classes with locks. It includes methods for parking threads, serving as replacements for the deprecated Thread.suspend() and Thread.resume() methods. Each thread has one permit: unpark(t) makes it available and park() takes it, blocking only if it is not there. So an unpark that comes before the park is not lost, unlike a notify with nobody waiting yet. The example measures both orders (park returns in 0 ms, wait sleeps through its whole timeout) and builds a one-shot signal the way the JDK's synchronizers use park: a volatile flag, park in a loop because it may return spuriously, and park(blocker) so the thread dump says what the thread waits for.

Examples

ReadWriteLock
ReentrantReadWriteLockCounter
ReentrantReadWriteLockCounter
AbstractOwnableSynchronizer
AbstractQueuedLongSynchronizer
AbstractQueuedSynchronizer
LockSupport

image

AbstractOwnableSynchronizer - A base class designed for creating synchronization mechanisms. It includes a simple getter/setter pair for storing and accessing an exclusive thread that can interact with the data. AQS extends it, and so does every lock built on AQS. The field has a second reader, the JVM: a thread dump lists every AbstractOwnableSynchronizer a thread owns under "Locked ownable synchronizers", and ThreadMXBean reports the same. The example writes one spin lock twice, on top of this class and with an owner field of its own. Both work, but only the first shows up in a dump, and a test checks that through ThreadMXBean.

AbstractQueuedSynchronizer - This class serves as the foundation for synchronization mechanisms in FutureTask, CountDownLatch, Semaphore, ReentrantLock, and ReentrantReadWriteLock. It can also be used to develop new synchronization mechanisms that rely on a single atomic integer value. AQS handles the queue of waiting threads, parking, timeouts and interrupts; a synchronizer only overrides a few hooks that say what the state means. In exclusive mode (tryAcquire, tryRelease) one thread holds it at a time. In shared mode (tryAcquireShared, tryReleaseShared) several can, as with Semaphore and CountDownLatch.

The example is a mutex made of three overridden hooks. Its tryRelease checks that the caller is the owner: without that check, any thread can call unlock() and let a second thread into the critical section while the first is still in it. The test for that fails if the check is removed.

AbstractQueuedLongSynchronizer - A variant of AbstractQueuedSynchronizer that supports operations on an atomic long value. The example is a byte budget in shared mode: the state is the number of free bytes, so the budget can exceed the roughly 2 GB an int-based Semaphore can count, and one release can let several waiters in.

Atomics

image

AtomicBoolean, AtomicInteger, AtomicLong, AtomicIntegerArray, AtomicLongArray - When you need to synchronize access to a simple int variable in a class, you can use synchronized constructs, or volatile with atomic set/get operations. However, the new Atomic* classes offer an even better solution. These classes use CAS (Compare-And-Swap) operations, which are faster than a lock: measured here about 2x uncontended and about 6x with eight threads, and LongAdder far more than that. See Benchmarks. Additionally, they provide methods for atomic addition, increment, and decrement.

AtomicReference - This class allows for atomic operations on an object reference. Its main use is keeping several values consistent with each other without a lock. The example is a range whose lower bound may never be above its upper. With one AtomicInteger per bound, each setter checks the other bound and then writes its own, so on [0, 10] setLower(5) and setUpper(4) can both pass their checks and leave [5, 4]: each step atomic, the pair not. The fix keeps both bounds in one immutable object and swaps it as a whole with compare-and-set. A setter that lost the race starts again from the new pair, checks again, and refuses. A read gets both bounds from one object too. The retry loop is what updateAndGet does, so the function passed to it can run more than once and must have no side effects.

AtomicMarkableReference - This class supports atomic operations on a pair of fields: an object reference and a boolean flag (true/false). It is made for the lock-free linked list (Harris, 2001). Removing a node with one CAS on its predecessor checks the predecessor and nothing else, so if another thread inserts right after the node meanwhile, the new key goes out with it, and if it removes the next node, that node comes back. The example forces both on a list with plain AtomicReference links. In the fixed list each next is an AtomicMarkableReference: remove() first marks the node's own next, which deletes it logically and makes every later CAS on that link fail, and only then unlinks it. A thread that finds a marked node on its way unlinks it. The mark has to change in the same CAS as the reference; a separate flag leaves the same gap.

AtomicStampedReference - This class supports atomic operations on a pair of fields: an object reference and an integer value. Its use is the ABA problem: a CAS checks that the value is still what the thread read, not that nothing happened in between, so a change from A to B and back to A goes unnoticed. The example forces that on a lock-free stack with reused nodes: a paused pop CASes the top from A to B after another thread popped A and B and pushed A back, and B, already popped, ends up on the stack again. With a stamp that every update increments, the paused CAS fails and the pop retries. In Java this needs node reuse, as in an object pool: with fresh nodes the paused thread's reference keeps A alive, so no other node can be the same A.

AtomicReferenceArray - An array of object references that can be updated atomically. A volatile array field is not the same thing: volatile covers the reference to the array, not its elements, and booking a seat ("take it if it is free") is a check and then a write, so two people who check at once are both confirmed. AtomicReferenceArray gives each element its own compareAndSet, so exactly one of them gets the seat, and seats never conflict with each other, as they would if the whole array were copied into one AtomicReference on every change. compareAndSet compares with ==, not equals(). The example's cancel relies on that: a late cancel that holds an old booking does not free the seat for a newer booking by the same person, although the two are equal records.

AtomicIntegerFieldUpdater, AtomicLongFieldUpdater, AtomicReferenceFieldUpdater - Atomic updates on an ordinary volatile field of any instance, through one static updater, instead of an Atomic* object per instance. That saves an object per counter, which counts when there are millions of instances, such as pooled buffers. The example is a reference-counted buffer. retain() is a compare-and-set loop, not getAndIncrement, because it must refuse a buffer whose count is already zero; a plain increment would bring a freed buffer back. The free action is taken with getAndSet(null), so when two frees race, it runs once. newUpdater checks its rules when it is created: the field must be volatile, of the exact type (int, not Integer), and accessible to the caller, so for a private field the updater lives in the declaring class. The rule it cannot check is that every write goes through it: a plain count++ elsewhere still loses updates.

VarHandle - Since Java 9, the modern way to do what the field updaters do, and what several of the classes above are built on. A handle to a field or an array element offers the same atomic operations with a choice of memory ordering: plain, opaque, release/acquire and volatile. The weaker modes are what the JDK's own queues use for a one-way handover, where a full volatile pays for an ordering nobody needs.

Examples

Atomics
Field updaters: a reference-counted buffer
AtomicMarkableReference
AtomicReferenceArray
AtomicReference
AtomicStampedReference
VarHandle
Release/acquire through a VarHandle

Testing

Run the suite with ./gradlew test; ./gradlew build runs it as part of the build. A JaCoCo coverage report is written to build/reports/jacoco/test/html/index.html.

Concurrency tests that lean on Thread.sleep pass on a fast machine and fail on a loaded CI runner, so these ones do not. They assert the actual condition - a latch reached, a counter settled, an ordering observed - through the helpers in src/test/java/org/alxkm/testsupport:

  • Await - polls a condition up to a timeout and fails with a clear message instead of hanging.
  • Concurrently - holds N threads behind a start gate and releases them together, so the operations actually overlap instead of running one after another; collect returns one result per thread.
  • UncaughtExceptions - a JUnit extension, registered for every test through autodetection (switched on in build.gradle), that fails a test when one of its threads dies of an uncaught exception. Without it the stack trace goes to stderr and the test passes: an example's main() could start two threads, both could fail, and mainRuns() stayed green. A task given to submit() is not covered; its Future keeps the exception, and the test has to call get().

Tests that pin down an antipattern assert both halves: that the broken version can actually lose updates, and that the corrected version never does.

Nine ways a concurrency test passes while the code is broken, every one of them found and fixed in this repository:

flowchart TD
    A["My concurrency test passes"] --> B{"Does it sleep<br/>instead of waiting?"}
    B -->|yes| B1["Passes fast, fails on CI.<br/>Wait for the condition."]
    B -->|no| C{"Does it assert inside<br/>a spawned thread?"}
    C -->|yes| C1["AssertionError kills that thread only.<br/>JUnit never sees it."]
    C -->|no| D{"Does it call the class<br/>it is named after?"}
    D -->|no| D1["It tests the JDK.<br/>407 lines here never mentioned<br/>the class under test."]
    D -->|yes| E{"Does it assert the<br/>outcome of a race?"}
    E -->|yes| E1["Passes, then fails next run.<br/>Order it with a latch."]
    E -->|no| F{"A hard threshold<br/>on a timing?"}
    F -->|yes| F1["Coin flip.<br/>Compare two strategies instead."]
    F -->|no| G["Probably a real test"]

The full list, with what each one looked like here: docs/diagrams/08-testing-concurrency.md.

Running tests

To run all tests:

./gradlew test

To run specific test categories:

# Run only pattern tests
./gradlew test --tests "org.alxkm.patterns.*"

# Run only anti-pattern tests  
./gradlew test --tests "org.alxkm.antipatterns.*"
Full test catalogue

Pattern Tests

Active Objects

Atomics

Balking Pattern

Collections

Double Checked Locking

Executors

Future

Guarded Suspension

Immutable Pattern

Monitor Object

Mutex

Odd-Even Printer

Fork/Join

Philosopher Problem

Reentrant Lock

Producer-Consumer Patterns

Queue

Read-Write Lock

Reactor Pattern

Scheduler

Semaphore

Synchronizers

Thread Local

Thread-Safe Lazy Initialization

Thread-Safe Builder Pattern

Concurrent Object Pool Pattern

Leader-Follower Pattern

Modern Java Features (Java 21+)

Virtual Threads Pattern

Structured Concurrency Pattern

Two-Phase Termination

Anti-Pattern Tests

Busy Waiting

Deadlock

Forgotten Synchronization

  • CounterTest.java - Both fixes lose no increment under contention; the unguarded counter never overcounts

Race Conditions

Thread Leakage

Double-Checked Locking

Excessive Synchronization

Lack of Thread Safety in Singletons

Lock Contention

Non-Atomic Compound Actions

Race Conditions (account balance)

Using Thread-Safe Collections Incorrectly

Contributing

Contributions are welcome - please open an issue or submit a pull request. When adding an example:

  • Put it under the topic package it belongs to, mirroring the existing layout.
  • Give the class a Javadoc comment explaining what it demonstrates, and, for an antipattern, why it breaks.
  • Add a test that asserts the behaviour rather than waiting for it.
  • Add a link to it in the matching README section.

CONTRIBUTING.md has the rest: how to run the stress tests and benchmarks, and what a test of concurrent code here may and may not assert.

License

This project is licensed under the MIT License - see the LICENSE file for details. Feel free to fork and modify these implementations for your own use cases.

Acknowledgments

This repository was inspired by multithreading technics and adapted for educational purposes. Some images source.

Explore projetos relacionados

Carregando…