Setup and TearDown executed by different threads

Aleksey Shipilev aleksey.shipilev at oracle.com
Wed Aug 13 11:25:25 UTC 2014


On 08/13/2014 03:06 PM, Dmitry Vyazelenko wrote:
> I've found a problem in multi-threaded benchmarks that @Setup and
> @TearDown are executed by different threads. This breaks the teardown
> code, because it assumes that same thread that executed @Setup will 
> execute @TearDown method.

If the @State is shared between the threads, you should not make
assumptions about who actually accessed the @State object and its
fixture methods.

> What I expect:
> - For Scope.Benchmark single thread will execute all @Setup/@TearDown methods
> (at least for Level.Trial and Level.Iteration, not sure about Level.Invocation though)

Ah, but that expectation is incorrect.

See
http://hg.openjdk.java.net/code-tools/jmh/file/60e8aae7ac7d/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_05_StateFixtures.java#l50.

"As with the State, fixture methods are only called by those benchmark
threads which are using the state." <-- That is, any thread which has
the access to @State can call the fixture method. This is analogous to
what you suggest from Scope.Group:

> - For Scope.Group probably use one of threads from the group? Or dedicated @Setup/@TrearDown
> thread created in addition.

Is there a convincing use case that shows we should track
@Setup/@TearDown callers for Scope.Benchmark?

Thanks,
-Aleksey



More information about the jmh-dev mailing list