rocketmq
rocketmq copied to clipboard
[improvement] use ThreadLocalRandom instead of Random
** Performance improvement **
Random is thread safe for use by multiple threads. But if multiple threads use the same instance of Random, the same seed is shared by multiple threads. It leads to contention between multiple threads and so to performance degradation.
ThreadLocalRandom is solution to above problem. ThreadLocalRandom has a Random instance per thread and safeguards against contention.
According to jmh benchmark,10 threads,AverageTime
RandomBenchmark.random: 2616.198 ns/op
RandomBenchmark.threadRandom: 3.764 ns/op