What is LongAdder?
This article shows you what is LongAdder, the content is concise and easy to understand, can definitely make your eyes bright, through the detailed introduction of this article, I hope you can get something.
It can be said that LongAdder uses space for time to make up for the bottleneck of AtomicLong.
The basic idea of LongAdder is to disperse hot spots. In AtomicLong, no matter how many threads are added to a value, LongAdder maintains not only a value (volatile long base) value, but also an array.
Transient volatile Cell [] cells; @ sun.misc.Contended static final class Cell {volatile long value; Cell (long x) {value = x;} final boolean cas (long cmp, long val) {return UNSAFE.compareAndSwapLong (this, valueOffset, cmp, val);}}
Although this array is indirectly maintained, this is not the point, we need to know that this array also maintains a value value, the purpose is very simple for cumulative use.
Different threads will hit different slots in the array, and each thread will only CAS the value in its own slot, thus achieving the purpose of hot spots dispersion.
When the concurrency is not high, the CAS is used to directly manipulate the base value, and when the concurrency is high, the CASbase may fail. After the failure, the value in the Cell [I] in the Cell [] array will be CAS by 1.
What is LongAdder above? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.