Why does this code run faster with a lock?

后端 未结 2 1538
孤街浪徒
孤街浪徒 2021-02-13 14:21

Some background: I created a contrived example to demonstrate use of VisualVM to my team. In particular, one method had an unnecessary synchronized

2条回答
  •  清酒与你
    2021-02-13 15:05

    The same problem as here.

    You're actually measuring the contention inside a PRNG with a shared state.

    JDKRandomGenerator is based on java.util.Random which has seed shared among all your worker threads. The threads compete to update seed in the compare-and-set loop.

    Why lock improves performance then? In fact, it helps to reduce contention inside java.util.Random by serializing the work: while one thread performs matrix multiplication, the other is filling the matrix with random numbers. Without lock threads do the same work concurrently.

提交回复
热议问题