Scope.Thread and @Setup bug?

Lev Serebryakov lev at
Wed Sep 28 13:02:54 UTC 2016

On 28.09.2016 10:20, Aleksey Shipilev wrote:

>>  Hmmm, I wrote this [code at the end of the message] and sometimes I got
>> exception from my setup method, that shows that it was called twice. My
>> ".registerReader()" and ".registerWriter()" checks, that they was called
>> only once, and sometimes it is not the case and my invariant check
> With @State(Scope.Thread), @Setup method would be called once by *each
> thread* that has a reference to a state object. Is that what you are
> seeing? If you have multiple threads calling e.g. writer(), then every
> one will have its own thread-local instance of {Reader,Writer}State, and
> try to register in your buf.

 I have only one writer thread and one reader thread (1 group, 2 threads
with different roles, as planned). Buffer is per-group (in per-group
state). So, everything should work. This is not 100% reproducible.
Sometimes JMH runs 5 warmup and 10 measurement iterations without
problem, and sometimes I got my exception on 3-4 warmup iteration (never
at first, but, as it is not-deterministic, maybe it is pure luck).

 I'll try to compile minimal example where I got this.

 Maybe, problem is that EVERY thread-local state setup is called at
EVERY thread? I've tried to construct benchmark in such way, that reader
and writer needs different states and I've hoped, that JMH will not
setup thread-local state which is not needed by this particular thread,
so I could distinguish between "reader" and "writer" threads, what I
need, because these threads need different setup. Are my assumptions

// Black Lion AKA Lev Serebryakov

More information about the jmh-dev mailing list