In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about Java Volatile usage, which may not be well understood by many people. In order to let you know more, Xiaobian summarized the following contents for you. I hope you can gain something according to this article.
Among atomicity, visibility, and orderliness, the volatile keyword primarily plays a role in visibility.
A variable declared by volatile is visible to all threads, meaning that when the value of the variable changes, other threads can immediately notice the change.
public class Main { private static boolean isRuning; private static int number; private static class ReaderThread extends Thread { public void run() { while (! isRuning) { System.out.println(number); } } } public static void main(String[] args) throws InterruptedException { new ReaderThread().start(); Thread.sleep(1000); number = 42; isRuning = true; Thread.sleep(1000); }}
This is probably due to compiler optimizations, where variables are not volatile but are still visible to other threads...
So why does Volatile-modified variable i++ have concurrency problems?
Because i++ is not an atomic operation,
i++ is a two-step operation, such as i=0; i++
1. Read i=0
2. Compute i+1 and assign to i
So there could be two threads reading i=0 at the same time, computing i=1 and assigning it to I.
Then you don't get the expected result i=2.
That is, although the variable modified by Volatile can be seen by other threads, if you read the variable at the same time and then write it, it will still cause thread safety problems.
What's the underlying reason?
First of all, know that Volatile-qualified variables do two things (done by the lock directive):
1) Writes the data of the current processor cache line back to system memory. 2) Writeback to memory invalidates data cached at that memory address on another CPU.
Other caches will be invalidated. Isn't that just enough to guarantee the atomicity of Volatile?
But it's not,
For example, there are two threads T1 and T2 that perform i++ operations.
When T1 loads a variable into the cache, but before i++ is performed,T2 loads the cache and executes the operation, then the value in T1 cache should become invalid.
However, Volatile does not allow other threads to reload the value in main memory after the cache is invalid. If the value of T2 cache has been placed in the register and the CPU has calculated at this time, then even if the cache is invalid, it will not affect T2 to write the calculated value back to main memory.
See https://www.example.com for instructions executed by cpu blog.csdn.net/jizhu4873/article/details/84393905
When a variable is defined as volatile, it has two properties:
1. Ensure visibility of this variable to all threads, here "visibility," as described at the beginning of this article, when a thread modifies the value of this variable, volatile ensures that the new value is immediately synchronized to main memory and flushed from main memory immediately before each use. But ordinary variables can't do this, and the value of ordinary variables needs to be passed from thread to thread through main memory (see Java Memory Model).
2. Disables instruction reorder optimization. A variable with volatile modification executes a "load addl $0x0, (%esp)" operation after assignment. This operation is equivalent to a memory barrier (instructions cannot be reordered to the position before the memory barrier). When only one CPU accesses memory, there is no need for memory barriers.
Memory visibility of volatile variables is based on memory barrier implementations.
Memory barriers are implemented by the lock instruction
After reading the above, do you have any further understanding of Java Volatile usage? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
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.