In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail that Volatile can not guarantee data synchronization in Java concurrent programming, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Validating volatile-decorated variables through an example does not guarantee their data synchronization.
The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. The thread's working memory keeps a copy of the main memory copy of the variables used by the thread. All operations on the variables (reading, assigning, etc.) must be carried out in the working memory, instead of directly reading and writing the variables in the main memory. Different threads can not directly access the variables in each other's working memory, and the transfer of variable values between threads needs to be completed through the main memory. The interaction among threads, main memory and working memory is shown in the figure.
When a variable is defined as volatile, it ensures the visibility of the variable to all threads, that is, when a thread modifies the value of the variable, the new value is immediately known to other threads. At this point, the read and write operation of the variable is completed directly in the main memory.
Volatile variables have the visibility properties of synchronized, but not atomic properties.
Volatile variables share the visibility features of synchronized, but none of the atomicity features.
Although an incremental operation looks like a separate operation, it is actually a combined operation consisting of a sequence of read-modify-write operations that must be performed atomically, and volatile does not provide the necessary atomic properties.
While the increment operation (x +) may look like a single operation, it is really a compound read-modify-write sequence of operations that must execute atomically-- and volatile does not provide the necessary atomicity.
In the multi-thread concurrent environment, the read / write operations of each thread may overlap. At this time, volatile can not guarantee data synchronization.
An example is given below:
Instance = > 500 threads run together, each thread sums 1 to 1000 times, and then adds a volatile shared variable value by 1. 0. When all 500 threads have completed the operation, the expected value is 500, because after each thread completes execution, the volatile variable is incremented by 1. 0.
Execute the program in a loop until the value of the volatile variable is less than 500, that is, the data is out of sync.
Public class NonSafeThread implements Runnable {/ * * share resources, adding 1 * / private volatile int volatileCount = 0 after each thread executes; public void run () {/ * * each thread calls the sum100 () method 1000 times * / 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: 221
*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.