Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is LongAdder?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report