Quick revision
The short answer
Beyond plain unit tests I use a set of tools, each for a different kind of problem. jcstress runs tiny concurrent snippets millions of times and reports which outcomes actually occurred, which is ideal for memory-model questions such as visibility and reordering. Lincheck from JetBrains checks a concurrent data structure against a simple sequential specification. Awaitility makes asynchronous assertions wait with a timeout instead of Thread.sleep. Designing code so that clocks and executors can be injected makes timing logic deterministic, and load tests at production-like concurrency find contention and pool problems that no unit test can.
What to remember
In this answer, we matched testing tools to the kind of concurrency risk. jcstress runs tiny snippets millions of times to reveal memory-model outcomes such as store-load reordering; Lincheck checks a concurrent data structure against its sequential behaviour and prints the failing interleaving; Awaitility replaces sleeps with bounded polling for asynchronous results; injecting Clock and Executor makes time and scheduling deterministic in tests; and load tests at production-like concurrency, with JMH for component benchmarks, find contention and pool saturation that only real load reveals.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Beyond plain unit tests I use a set of tools, each for a different kind of problem. jcstress runs tiny concurrent snippets millions of times and reports which outcomes actually occurred, which is ideal for memory-model questions such as visibility and reordering. Lincheck from JetBrains checks a concurrent data structure against a simple sequential specification. Awaitility makes asynchronous assertions wait with a timeout instead of Thread.sleep. Designing code so that clocks and executors can be injected makes timing logic deterministic, and load tests at production-like concurrency find contention and pool problems that no unit test can.
Why this question is asked
Concurrent code is hard to test because a bug needs a particular interleaving of threads, and an ordinary test explores only the interleavings that happen to occur. Teams that rely only on unit tests end up with flaky tests full of sleeps and with bugs that appear only in production. Interviewers ask this to see whether we know the specialised tools, and, more importantly, which tool fits which kind of risk. In this answer, we will go through the tools from the lowest level to the highest: jcstress for memory-model behaviour, Lincheck for data structures, Awaitility for asynchronous code, injectable time and executors for deterministic tests, and load tests for system-level problems.
1Why Plain Unit Tests Are Not Enough
A unit test runs code once with fixed inputs and checks the output. For concurrent code, the input also includes the schedule: which thread runs which instruction when. A unit test does not control the schedule, so it tests one or a few arbitrary schedules. Worse, the problems differ by level. A visibility bug in a lock-free flag, a linearizability bug in a concurrent map, a test that waits for an asynchronous event, and a pool that saturates at 500 users each need a different kind of tool.
| Tool or technique | Good for | Not good for |
|---|---|---|
| jcstress | Visibility, reordering, atomicity of tiny snippets | Business logic, large code |
| Lincheck | Concurrent data structures vs a sequential model | Code with I/O or side effects |
| Awaitility | Asserting on async results without sleep | Finding races |
| Injected Clock and Executor | Deterministic timing and scheduling | Real-world contention |
| Load tests | Contention, pool saturation, latency | Pinpointing a single race |
2Jcstress: What Can Actually Happen?
jcstress, the Java Concurrency Stress harness from OpenJDK, answers the question 'which results are possible when these threads run this code at the same time?' We write a small test class with actor methods, each run by its own thread, and a result object. The harness runs the actors millions of times with different thread arrangements and compiler modes and counts each outcome. We annotate which outcomes are acceptable, which are interesting, and which are forbidden.
Notice the result 0, 0: each thread wrote its own field and then read the other's, yet both read 0. That is a store-load reordering, which the Java memory model permits for plain fields and which real CPUs perform. Making x and y volatile forbids it, and jcstress would then report 0, 0 as never seen. This is the kind of question no unit test can answer reliably, and it is why jcstress is the tool for authors of low-level code: custom locks, lock-free structures, double-checked locking and safe publication.
3Lincheck: Is My Data Structure Linearizable?
Lincheck, from JetBrains, works at the next level up. We give it a concurrent data structure and a list of its operations; it generates random scenarios of concurrent operations, runs them, and checks whether every result could have come from some sequential order of the same operations. That property is called linearizability, and it is the correctness definition for concurrent collections. Lincheck has a stress mode and a model-checking mode that systematically explores interleavings and, on failure, prints the exact interleaving that broke the structure.
Lincheck is primarily a Kotlin library but works from Java tests. It is the right tool when we build our own concurrent collection, cache or counter, and much more thorough than a hand-written stress test, because it compares results against the sequential behaviour automatically.
4Awaitility: Waiting for Asynchronous Results
Much of our concurrency is asynchronous rather than low-level: a message is published and a listener updates the database a moment later, or an @Async method completes in the background. Tests for that code often contain Thread.sleep(2000), which is both slow, since it always waits the full time, and flaky, since on a loaded CI machine two seconds may not be enough. Awaitility replaces the sleep with polling until a condition holds or a timeout passes.
So the test finishes as soon as the condition is true, and it gives the system plenty of slack under load. Awaitility does not find races; it makes tests of asynchronous behaviour fast and stable.
5Designing for Deterministic Tests
The most effective technique is not a tool but a design choice. Code that calls Instant.now(), Thread.sleep() or creates its own thread pools is hard to test, because time and scheduling are hidden inside it. If instead the class receives a java.time.Clock and an Executor through its constructor, a test can pass a fixed or manually advanced clock and an executor that runs tasks immediately on the calling thread. A retry with backoff, a cache expiry or a timeout can then be tested in milliseconds, with the same result every time.
6Load Tests for the Problems Only Real Concurrency Shows
Finally, some problems exist only at the level of the whole service under real load: lock contention that grows with traffic, connection pool exhaustion, thread pool saturation, and timeouts that cascade. No unit-level tool sees them. A load test with Gatling, k6 or JMeter, run at production-like concurrency against a production-like environment, with JFR recording in the service, finds them before release. JMH, the OpenJDK microbenchmark harness, helps at the component level when we need to compare, for example, a synchronized map with a ConcurrentHashMap under 32 threads.
7Follow-up questions
When is jcstress worth the effort for an application team?
Rarely for ordinary business code, which should use well-tested library constructs such as ConcurrentHashMap, atomics and executors. It is worth it when someone writes low-level synchronization themselves, such as a lock-free structure, a custom publication scheme, or double-checked locking, and especially for shared libraries used by many teams.
Can Awaitility hide real bugs by waiting too long?
It can hide performance regressions if the timeout is generous, since a confirmation that took 4.9 seconds still passes a 5-second wait. For correctness that is fine. If timing matters, we assert on it separately, for example with a metric in a load test rather than in a functional test.
What is linearizability, in plain words?
It means every operation appears to take effect at a single instant between its call and its return, so the results of concurrent operations match some one-at-a-time order consistent with real time. A queue is linearizable if, however threads interleave, what each poll returns could have come from some sequence of the same offers and polls. Lincheck checks exactly this.
8Conclusion
In this answer, we matched testing tools to the kind of concurrency risk. jcstress runs tiny snippets millions of times to reveal memory-model outcomes such as store-load reordering; Lincheck checks a concurrent data structure against its sequential behaviour and prints the failing interleaving; Awaitility replaces sleeps with bounded polling for asynchronous results; injecting Clock and Executor makes time and scheduling deterministic in tests; and load tests at production-like concurrency, with JMH for component benchmarks, find contention and pool saturation that only real load reveals.
Open with
I think about which kind of risk I am testing, because each tool covers a different level. For low-level code where the memory model matters, like custom lock-free structures, safe publication or double-checked locking, I use jcstress from OpenJDK.
Then, if they want more
I write small actor methods that run concurrently and declare which outcomes are acceptable, and it runs them millions of times and reports every outcome it saw, so it can show something like a store-load reordering on plain fields that no unit test would reliably catch. For a concurrent data structure I would use Lincheck from JetBrains.
I list the operations, and it generates concurrent scenarios and checks that the results match some sequential order, which is linearizability; in model-checking mode it prints the exact interleaving that fails.
For asynchronous code, like event listeners or @Async methods, I use Awaitility instead of Thread.sleep, so the test polls until the condition holds or a timeout passes, which makes it both faster and less flaky.
A big one is design: if classes take a Clock and an Executor in their constructor, tests can use a controllable clock and a direct executor, and retries, expiries and timeouts become deterministic.
Finally, contention, pool exhaustion and saturation only appear at real concurrency, so critical services get load tests at production-like levels, with JFR running, and JMH for comparing components.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What tools and techniques would you use to test concurrent Java code beyond plain unit...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/d1a5651ee9/