Often volatile in Java multithreaded programming, sometimes this keyword is confused with synchronized or lock, specifically resolves as follows:
A member variable visibility problem exists in a multithreaded environment: There is a line stacks memory space for every thread in Java. The memory space holds the variable information for the thread at run time, and when the thread accesses a variable value, it first finds the object's heap memory or the specific contents of the stack dump (native data type) based on the variable's address. The same assignment is then stored in the thread stack of the threads, and then all operations on the variable are not related to the contents of the variables in the stack memory until the thread completes exiting, and the operation is a copy of its own line Cheng. When the operation is finished, the result of the operation is written back into main memory. If there are two threads A and B, a colleague operates a variable x; A adds 1 to x, then B gets a copy that might be X plus 1, or x, to ensure that the most recent data variable in memory needs to be added with the volatile keyword. In this way, every time the X is operated to check the value of the Cheng variable is not the same as the value of the variable in memory, if not the same will be the load again.
Eg:
public class Threadsee {//T1 thread will do the corresponding action based on the value of flag, the main thread will change the value of the T1 public static void main (STR
Ing[] args) throws interruptedexception {threadtest th= new threadtest ();
thread T1 = new thread (th);
T1.start ();
Thread.Sleep (1000);
Th.changeflag ();
Thread.Sleep (2000);
System.out.println (Th.getflag ()); The class ThreadTest implements runnable{//thread accesses the variable to load it into the corresponding thread stack, acquiring the latest data in memory each time private volatile bool
EAN Stopflag;
@Override public void Run () {int i=0;
while (!stopflag) {i++;
System.out.println ("= =" +thread.currentthread (). GetName ());
} System.out.println ("Thread finish:" +i);
public void Changeflag () {this.stopflag=true;
System.out.println (Thread.CurrentThread (). GetName () + "***********");
public Boolean Getflag () {return stopflag; }
}
The above code, if removed volatile, will continue to die loop execution.
But volatile does not guarantee thread-safe synchronization
eg
public class Threadsave implements runnable{
static threadsave sync = new Threadsave ();
static volatile int j=0;
Lock lock =new Reentrantlock ();
public void Inscane () {
//Lock.lock ();
for (int i=0;i<10000000;i++) {
j + +;
} Lock.unlock ();
}
@Override public
Void Run () {
inscane ();
}
public static void Main (string[] args) throws interruptedexception {
thread t1 = new thread (sync);
Thread t2 = new Thread (sync);
T1.start ();
T2.start ();
T1.join ();
T2.join ();
System.out.println (j);
}
The result of this code execution is not expected to be 20000000,
Because for volatile-decorated variables, the JVM virtual machine only guarantees that the values loaded from the main memory to the working memory of the thread are up to date.
For example, if thread 1, thread 2 in the line stacks and main memory read,load operation, found that the value of the main memory count is 5, then the latest value will be loaded
After the thread 1 heap count is modified, it is write into main memory, and the count variable in main memory becomes 6;
Thread 2 because the read,load operation has already been performed, the variable value of the main memory count is also updated to 6 after the operation is performed;
When two threads are modified with the volatile keyword in a timely manner, concurrency is still present.
To sum up:
volatile will only ensure that threads do a check of the current line stacks variable value and the main memory data value is the same as such an action, that is it. Lock or synchronized will ensure that only a single thread enters the method at a given moment, ensuring its thread security.
So there is no practical logic meaning if multiple threads are to modify a volatile variable. This is useful if a thread modifies the value of the variable that other threads depend on.
The above is the entire content of this article, I hope to help you learn.