In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use the volatile keyword in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is the principle of 1.volatile to achieve visibility?
Shared variables decorated with volatile variables have an extra Lock prefix instruction in the assembly code when writing.
Under this instruction, a multicore processor causes two things:
Writes the data of the current processor cache row back to system memory
This write-back operation invalidates data that has that memory address cached in other CPU
Here we need to briefly understand the CPU cache consistency problem: in a multi-core processor environment, each CPU has its own cache line and caches data in memory. To maintain data consistency in multiple CPU caches, two problems need to be solved:
One is to write and propagate (when the cache data in one CPU is updated, it needs to be propagated to the cache of other CPU)
Second, the serialization of transactions (changes to data in one CPU appear to be in the same order in other CPU, that is, the concept of approximate lock is introduced to ensure that only one CPU can modify the data at a time)
Write propagation is accomplished through bus sniffing: event broadcasts of modified data are notified to all other cores through the bus, and each CPU core listens for broadcast events on the bus and checks whether there is the same data in its own Cache, while transaction serialization is done through the MESI protocol.
In MESI (Modified (modified), Exclusive (exclusive), Shared (shared), Ivalidated (invalid) protocols, if you want to modify a shared data and cannot modify it directly, you must first broadcast a request to other CPU. After changing the corresponding data status in other CPU cache to Invalidated;, other CPU needs to force reading data marked as Invalidated from memory.
two。 Demonstrate the visibility of volatile public class VolatileDemo {static int flag = 1; / / define a shared variable public static void main (String [] args) {/ / two threads, one thread is responsible for reading the value of flag and the other thread is responsible for modifying the value of flag new Thread () {int localflag = flag @ Override public void run () {while (true) {/ / flag is different from localflag after it is modified. If (localizable additional flag) {System.out.println ("read the modified value of flag:" + flag) / / assign the read value to the local variable localflag = flag;} .start (); new Thread () {int localflag = flag @ Override public void run () {while (true) {/ / keep modifying the value of flag System.out.println ("modify the value of flag:" + localflag); flag = localflag / / dormant for one second to better observe the result try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();} .start ();}}
You can see that another thread cannot read the modified value in time.
After the shared variable is decorated with volatile:
Public class VolatileDemo {static volatile int flag = 1; public static void main (String [] args) {/ / two threads, one thread is responsible for reading the value of flag, and the other thread is responsible for modifying the value of flag new Thread () {int localflag = flag @ Override public void run () {while (true) {/ / flag is different from localflag after it is modified. If (localizable additional flag) {System.out.println ("read the modified value of flag:" + flag) / / assign the read value to the local variable localflag = flag;} .start (); new Thread () {int localflag = flag @ Override public void run () {while (true) {/ / keep modifying the value of flag System.out.println ("modify the value of flag:" + localflag); flag = localflag / / dormant for one second to better observe the result try {Thread.sleep (1000);} catch (InterruptedException e) {e.printStackTrace ();} .start ();}}
You can see that after being decorated with volatile, another thread can always read the modified value each time.
This is the end of the content of "how to use the volatile keyword in java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.