Java Concurrency Interview

Testing concurrent code question 163 of 180 senior

What is jcstress and when do you reach for it?

jcstress writes the test for you in a shape ordinary code cannot: two @Actor methods run with no synchronisation between them, the harness repeats them for millions of samples, shuffles JIT decisions between forks, and records the frequency of every observed result. Outcomes are labelled ACCEPTABLE, ACCEPTABLE_INTERESTING or FORBIDDEN, so the test fails if a guarantee is ever violated.

Use it to answer questions of the form "can this be observed at all": does this idiom need volatile, is this publication safe, can this field be seen at its default. Its output is also the most persuasive artefact you can put in a code review, because it replaces an argument with a frequency table.

What it is not: a test for your business logic. It is slow, deliberately, and belongs in a separate source set that CI compiles but does not run on every build.

testing jcstress memory-model

Quiz yourself on Testing concurrent code Suggest a correction