In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Today Xiaobian to share with you why the use of LongAdder rather than volatile related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after some gains, let's learn about it together.
Note: If count++ operation, use the following class implementation: AtomicInteger count = new AtomicInteger(); count.addAndGet(1); if JDK8, recommend using LongAdder object, better performance than AtomicLong (reduce the number of optimistic lock retries).
There are two main points in the above:
volatile cannot be used in scenarios such as count++ that are not write-at-one and read-at-many;
If JDK8 recommends LongAdder instead of AtomicLong instead of volatile, LongAdder has better performance.
However, words are not valid. Even if it is said by a lone boss, we have to prove it, because Old Master Ma once said: Practice is the only criterion for testing truth.
This also has its benefits, first, deepen our knowledge of knowledge; second, the document only wrote LongAdder performance is higher than AtomicLong, but how much higher? It doesn't say that we can only test it ourselves.
Not much to say, next we go directly to the official content of this article...
volatile thread safety test
First, let's test volatile's thread safety in a multi-write environment. The test code is as follows:
public class VolatileExample { public static volatile int count = 0; //counter public static final int size = 100000; //Number of loops public static void main(String[] args) { // ++ mode 10w times Thread thread = new Thread(() -> { for (int i = 1; i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.