rocketmq icon indicating copy to clipboard operation
rocketmq copied to clipboard

[improvement] use ThreadLocalRandom instead of Random

Open binglind opened this issue 5 years ago • 0 comments

** 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

binglind avatar Nov 21 '19 07:11 binglind