Sub topic 15
Benchmarking with multiple threads
This chapter assumes you can write a basic @Benchmark method, use @State scopes, and run target/benchmarks.jar. It covers how to measure code that runs on several threads at once.
Why threads change the result
Some code behaves differently under contention. Several threads that update one shared counter wait on each other, and a single-threaded benchmark never shows that cost. JMH can run the same benchmark method on many threads at the same time.
By default JMH uses one thread. The @Threads annotation changes that.
Setting the thread count
Put @Threads on a method or a class. The value Threads.MAX uses one thread per available processor.
package org.sample;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)
public class CounterBenchmark {
private final AtomicLong atomic = new AtomicLong();
private final LongAdder adder = new LongAdder();
@Benchmark
@Threads(4)
public long atomicIncrement() {
return atomic.incrementAndGet();
}
@Benchmark
@Threads(4)
public void adderIncrement() {
adder.increment();
}
}Both methods run on four threads. Because the state uses Scope.Benchmark, all four threads share one AtomicLong and one LongAdder. This is the setup that exposes contention. LongAdder spreads updates across internal cells, so it usually scores higher under contention.
You can also set the count at run time, which overrides the annotation:
java -jar target/benchmarks.jar CounterBenchmark -t 4Choosing the state scope
The scope you pick decides what the threads share.
| Scope | Instances | Use it for |
|---|---|---|
| `Scope.Benchmark` | One, shared by all threads | Measuring contention on shared data |
| `Scope.Thread` | One per thread | Independent work with no sharing |
| `Scope.Group` | One per thread group | Producer and consumer style tests |
Reading multithreaded scores
In Mode.Throughput, the score is the combined operations per unit of time across all threads. In Mode.AverageTime, the score is the average time per operation. If throughput does not rise when you add threads, the code is probably limited by contention, by memory bandwidth, or by too few physical cores.
To see how the code scales, run the same benchmark with -t 1, -t 2, -t 4 and so on, then compare the scores.
Asymmetric workloads with groups
Sometimes threads should do different jobs, such as one writer and several readers. Use @Group to give methods a shared group name, and @GroupThreads to set how many threads run each method. The state needs Scope.Group for the group's threads to share it.
Exercise
A benchmark uses @State(Scope.Thread) and @Threads(8) to test a shared ConcurrentHashMap field declared in that state class. Why will the result not show contention, and what change fixes it?
Show answer
With Scope.Thread, JMH creates a separate state object for each thread, so each thread works on its own map and no thread competes with another. Change the scope to Scope.Benchmark so all eight threads share a single map.