In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the similarities and differences between volatile and atomic class". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the similarities and differences between volatile and atomic class".
Volatile and atomic classes
Let's first look at a case. As shown in the figure, we have two threads.
As you can see in the upper-left corner of the figure, there is a common boolean flag tag bit, which is initially assigned as true.
Thread 2 then enters a while loop and decides whether to continue or exit based on the value of the flag, the tag bit.
At first, because the value of flag is true, the loop will be executed here for a certain period of time. Then suppose that at some point thread 1 changes the value of this flag to false, and what it wants is for thread 2 to stop running when it sees this change.
But this is actually risky. Thread 2 may not stop immediately, it may take a while to stop, or even in the most extreme cases it may never stop.
To understand why this is happening, let's first take a look at the memory structure of CPU. Here is a simple diagram of a dual-core CPU:
As you can see, Thread 1 and Thread 2 run on different CPU cores, each with its own local memory and its shared memory underneath.
At first, they can all read that flag is true, but when thread 1 is changed to false, thread 2 cannot see the change in time, because thread 2 does not have direct access to thread 1's local memory. This problem is a very typical visibility problem.
To solve this problem, we just need to add the volatile keyword in front of the variable, as long as we add this keyword, and every time the variable is modified, other threads will see it, so that once thread 1 changes this value, thread 2 can see it immediately, so it can exit the while loop.
The reason why you can add a keyword to let it have visibility, because with this keyword, thread 1 changes will be flush into shared memory, and then be refresh to thread 2's local memory, so thread 2 can feel the change, so the keyword volatile is mainly used to solve the visibility problem, can ensure thread safety to a certain extent.
Now let's review the familiar scenario of multithreaded simultaneous value++, as shown in the figure:
If it were initialized to add 1000 times per thread, the end result would probably not be 2000. Because value++ is not atomic, thread safety problems can occur in the case of multithreading. But if we use the volatile keyword here, can we solve the problem?
Unfortunately, even using volatile does not guarantee thread safety, because the problem here is not just visibility, but also atomicity.
There are several ways to solve the problem here. The first is to use the synchronized keyword, as shown in the figure:
In this way, two threads cannot change the value of value at the same time, ensuring the atomicity of the value++ statement, and synchronized also ensures visibility, that is, when the first thread modifies the value value, the second thread can see the result of this modification immediately.
The second way to solve this problem is to use our atomic class, as shown in the figure:
For example, use an AtomicInteger, and then each thread calls its incrementAndGet method.
Instead of locking atomic variables, we can use its incrementAndGet method, which is atomically guaranteed by CPU instructions at the bottom, so there are no thread safety problems even if multiple threads are running at the same time.
Usage scenarios for atomic classes and volatile
We can see that the usage scenarios of volatile and atomic classes are different. If we have a visibility problem, we can use the volatile keyword, but if our problem is a combined operation that requires synchronization to solve the atomicity problem, then we can use atomic variables instead of the volatile keyword.
In general, volatile can be used to modify boolean type tag bits, because for tag bits, the direct assignment itself is atomic, coupled with volatile ensuring visibility, so it is thread-safe.
Thank you for your reading, the above is "what are the similarities and differences between volatile and atomic class" content, after the study of this article, I believe you have a deeper understanding of the similarities and differences between volatile and atomic class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.