(The main content of this article is from "Java Multithreaded Programming core technology")
The main purpose of the volatile keyword is to ensure the visibility of variables between threads.
PackageCom.func; Public classRunthreadextendsthread{Private BooleanIsRunning =true;//volatile Private Boolean isrunning = true; Public Booleanisrunning () {returnisrunning; } Public voidSetrunning (Booleanisrunning) { This. isrunning =isrunning; } @Override Public voidrun () {System.out.println ("Into the Run!" "); while(IsRunning = =true) {} System.out.println ("Stop running!" "); } }
Packagecom.test;ImportCom.func.RunThread; Public classTest3 { Public Static voidMain (string[] args) {Try{runthread Runthread=NewRunthread (); Runthread.start (); Thread.Sleep (1000); Runthread.setrunning (false); System.out.println ("IsRunning has been assigned to false"); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } }}
The following condition occurs when the JVM is set to-server
Can see SYSTEM.OUT.PRINTLN ("Stop running!"). "); Does not get run, that is, IsRunning has been a state of true. Runthread.setrunning (FALSE); This sentence does not play a corresponding role.
So why is that?
This is due to inconsistent values in the private stack and the public stack. When the JVM is set to-server, the thread will always take a value in the private stack in order to perform efficiency. Runthread.setrunning (false); The isrunning in the public stack is set to false, but isrunning in the private stack is not synchronized.
In this case, you can use the volatile keyword solution. The isrunniing variable is decorated with the volatile keyword.
volatile Private Boolean true;
The volatile keyword forces the thread to get the value of the variable from the public stack, rather than getting the value of the variable from the private stack.
Use the volatile keyword to increase the visibility of strength variables across multiple threads. But the volatile keyword only guarantees that visibility does not guarantee atomicity. You cannot use the volatile keyword to ensure thread safety .
The following example shows that the volatile keyword does not guarantee thread safety.
PackageCom.func; Public classMyThreadextendsthread{volatile Public Static intcount;//public static int count; Private Static voidAddcount () {//synchronized private static void Addcount () { for(inti = 0; I < 100; i++) {Count++; } System.out.println ("Count =" +count); } @Override Public voidrun () {addcount (); }}
Packagecom.test;ImportCom.func.MyThread; Public classTest { Public Static voidMain (string[] args) {Try{mythread[] mythreads=Newmythread[100]; for(inti = 0; I < 100; i++) {Mythreads[i]=NewMyThread (); } for(inti = 0; i < mythreads.length; i++) {Mythreads[i].start (); } } Catch(Exception e) {//Todo:handle Exception } }}
You can see that the final result is not 10000, and seeing volatile does not guarantee thread safety. Although the data is taken out of the public stack, the modification after the data is removed is not an atomic operation (the count++ is not atomic), so the data is not what we want when the modified data is finally synchronized.
To ensure the atomicity of the data, we still need to use the Synchronized keyword. The Synchronized keyword guarantees atomicity and resolves the synchronization of access resources between multiple threads.
synchronized Private Static void
...
}
Java Multithreaded Programming--volatile keywords