Concurrent use of Java util. Random contention
•
Java
Oracle Java documentation says:
Could it be the reason for poor performance?
Solution
Internally, Java util. Random keeps atomiclong from the current seed and competes to update the seed whenever a new random number is requested
From Java util. Random implementation:
protected int next(int bits) { long oldseed,nextseed; AtomicLong seed = this.seed; do { oldseed = seed.get(); nextseed = (oldseed * multiplier + addend) & mask; } while (!seed.compareAndSet(oldseed,nextseed)); return (int)(nextseed >>> (48 - bits)); }
On the other hand, threadlocalrandom ensures that the seed is updated without any contention by having a seed per thread
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码