In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the role of the volatile keyword in Java concurrent programming". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the role of the volatile keyword in Java concurrent programming"!
1. What is volatile?
First of all, briefly, what is volatile? Volatile is a keyword in Java and a synchronization mechanism. In order to ensure the visibility of variables in volatile, variables modified by volatile are shared. Modified the variables modified by volatile so that other threads can read the latest values.
2. Three characteristics of concurrent programming
There are three important features of concurrent programming: atomic rows, visibility, and ordering.
Atomicity: atomicity refers to one or more operations, either all executed without being interrupted by other operations, or none of them are performed.
Visibility: visibility means that shared variables are visible to multiple threads, that is, if one thread modifies the variable, other threads will know immediately.
Orderliness: orderliness means that the execution order of the program is executed according to the sequence of the code.
3. What does order reorder mean?
If we write a program, we will expect that the actual execution of these statements will be consistent with the order of the code, in most cases, but in fact, the compiler, JVM, or CPU may adjust the order of execution for the purpose of optimization, that is, instruction reordering.
Benefits of reordering: increased processing speed
The code sequence is shown in the figure:
After instruction rescheduling, aq100; a = aq100 will be mentioned to execute together to improve efficiency.
In the above example, the execution efficiency can be improved, but sometimes instruction rearrangement can cause problems. The following code example shows that the code order is to initialize content and then set the identity to true. Thread B calls the content method after it detects that it is true.
If the instruction is rearranged and the initialization is not completed, call the method of conten directly.
Therefore, instruction rearrangement has both advantages and disadvantages. Generally, instruction rearrangement may be carried out by cpu, compiler or memory. In order to avoid instruction rearrangement and ensure the order of concurrent programming, sometimes it is necessary to use synchronized or volatile to avoid. Volatile can avoid instruction rearrangement, ensure the order of concurrent programming, and depend on the memory barrier of the operating system.
4. What is the function of volatile?
It can also be specified from the previous study that the volatile keyword ensures the order and visibility of concurrent programming.
Ensure visibility
Volatile ensures visibility:
When using the volatile variable, you must reload from the main memory to the working memory. After modifying the volatile variable, you must immediately synchronize it back to the main memory
Visibility is related to the Java memory model. For details, please refer to my previous blog: links
Java memory model (JMM) structure chart, each Java thread has its own working memory, volatile modified variables, after modification, will be automatically synchronized to the main memory; each thread will first read from the main memory to the working memory copy
Note: volatile can only guarantee the visibility of variables, but not for a Java object. Set volatile to the specific properties of the object.
Guarantee order
The orderliness of concurrent programming has been described in detail before, mainly because cpu, jvm and memory all reorder the code execution order, plus volatile can ensure orderliness and avoid instruction rearrangement, depending on the memory barrier of the operating system.
5. Volatile can guarantee atomicity.
Volatitle can only guarantee the atomicity of a single variable, not the atomic operation of a series of operations, so volatile is thread-unsafe and does not have atomicity.
6. Comparison between volatile and synchronized
Volatile can not guarantee thread safety, synchronized can guarantee thread safety volatile is lightweight, and there is no lock mechanism, the performance is better than synchronized volatile is not atomicity, synchronized can guarantee atomicity
At this point, I believe you have a deeper understanding of "what is the role of the volatile keyword in Java concurrent programming". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.