In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to understand the Java Volatile keyword, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Text
Before we talk about Volatile, let's review the three elements of the Java memory model: atomicity, visibility, and ordering, that is, the three elements of concurrent programming that are often mentioned.
Three elements of concurrent programming 1. Atomicity
Like atomicity in database transactions, operations that satisfy atomicity are uninterruptible, either successful or failed.
Only simple reading and assignment (and must assign a number to a variable, and the mutual assignment between variables is not an atomic operation) is an atomic operation.
For example:
I = 2b / j = I / I = I + 1
In the above four operations, inotify 2 is a read operation, must be an atomic operation, jroomi you think is an atomic operation, in fact, it is divided into two steps, one is to read the value of I, and then assign a value to j, this is a 2-step operation, not an atomic operation, inotify + and I = I + 1 are actually equivalent, read the value of I, add 1, and then write back to main memory, that is a three-step operation.
So in the above example, the final value may occur in a variety of cases, just because it does not satisfy atomicity.
Non-atomic operations will have thread safety problems, we need to use synchronization technology (sychronized) to make it an atomic operation, java's concurrent package provides some atomic classes: for example: AtomicInteger, AtomicLong, and so on.
two。 Visibility
When multiple threads access the same shared variable, one thread modifies the value of the shared variable, and the other thread can get the modified value immediately.
3. Order
The compiler and processor reorder the instruction sequence in order to optimize program performance, that is, the order of the code you write is inconsistent with the order in which the instructions are executed. However, the reordering process will not affect the execution of single-threaded programs, but will affect the correctness of multithreaded concurrent execution.
Volatile
Volatile is a type modifier of the Java language, and once a shared variable (member variable of a class, static member variable of a class) is modified by Volatile, it has two layers of semantics:
1. Ensure visibility under multithreading
2. Instruction reordering is prohibited (that is, order is guaranteed)
One thing to note here is that Volatile can only make the content he embellishes visible and orderly. Volatile can only guarantee atomicity for a single read / write, and the operation of iTunes + does not guarantee atomicity.
Memory Model of Volatile
The * * Java memory Model (JMM) * * is an abstract concept that does not really exist. It describes a set of rules or specifications that define how variables in the program (including instance fields, static fields, and elements that make up an array object) are accessed.
This paper attempts to shield the memory access differences of various hardware and operating systems, so as to achieve the consistent memory access effect of Java programs on various platforms.
The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. The working memory of the thread keeps a copy of the main memory copy of the variables used in the thread. All the operations of the thread on the variables must be carried out in the working memory, and can not read or write the main memory directly. There is no direct access to variables in each other's working memory between different threads, and the transfer of variables between threads requires data synchronization between their own working memory and main memory.
The main memory mainly stores Java instance objects, and all instance objects created by threads are stored in main memory, regardless of whether the instance object is a member variable or a local variable in the method (also known as local variable). Of course, it also includes shared class information, constants, and static variables. Because it is a shared data region, multiple threads accessing the same variable may find thread safety problems.
Working memory each thread has its own working memory (Working Memory, also known as local memory, which can be compared to the processor cache described earlier), and the thread's working memory holds a copy of the shared variable in the main memory of the variable used by the thread.
Working memory is an abstract concept of JMM and doesn't really exist. It covers caching, write buffers, registers and other hardware and compiler optimizations.
Mainly stores all the local variable information of the current method (a copy of the variable in the main memory is stored in the working memory). Each thread can only access its own working memory, that is, the local variables in the thread are not visible to other threads. even if two threads are executing the same piece of code, they will each create local variables that belong to the current thread in their own working memory. Of course, it also includes bytecode line number indicators and information about Native methods.
The realization principle of Volatile
Volatile ensures memory visibility
There is a specific interaction protocol between main memory and working memory. JMM defines eight operations that are atomic and inseparable. They are: lock,unlock,read,load,use,assign,store,write.
Among them, lock, unlock, read, write act on the main memory; load, use, assign, store act on the working memory.
(1) lock
Locks the variables in the main memory and is exclusive to a thread.
(2) unclock
The lock added by lock is unlocked, and other threads have a chance to access this variable.
(3) read
Read the variable values in the main memory into the working memory.
(4) load
Save the values read by read to a copy of the variable in working memory.
(5) use
Pass the value to the thread's code execution engine.
(6) assign
Reassigns the value returned by the execution engine processing to the variable copy.
(7) store
Stores the value of the copy of the variable in main memory.
(8) write
Writes the value stored by store to a shared variable in main memory.
Copy variables from main memory to current working memory (read and load)
Execute code to change the value of a shared variable (use and assign)
Refresh main memory related content with working memory data (store and write)
Instruction rule
Read and load, store and write must appear in pairs.
Assign operation, the working memory variable must be brushed back to the main memory after the change.
Only one thread can run at a time to lock the variable. The current thread lock can be reentered, and the number of unlock must be equal to the number of lock before the variable can be unlocked.
After lock a variable, it clears the value of the thread's working memory variable and re-executes the load or assign operation to initialize the value of the variable in the working memory.
Before unlock, variables must be synchronized to main memory (store/write operation).
Volatile source code case
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow 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.