Java multi-threading of credit to build rental volatile keyword and disk memory barrier

volatile synchronization mechanism is one of the most lightweight JVM provided credit to build rental disc Q [1152880099] Java memory model is volatile because the definition of special access rules, making it possible to achieve two major features in Java memory model: Visibility and orderliness. Because of the volatile keyword has these two properties, so we can use the volatile keyword solve some synchronization of multiple threads.

1, volatile visibility
visibility volatile means that when a variable is volatile after modification, this variable is visible to all threads. That point vernacular When a thread modifies a volatile variables, other threads can know immediately modify this variable, get the most recent value of this variable.

Threads in Java memory model mentioned before in conjunction with an article, the interactive relationship between working memory, main memory, so we can understand the visibility of the volatile is defined as volatile variables, write to it when the thread the value will not cached in the working memory, but directly to the modified value is written back to main memory refresh, and when the monitoring processor to the memory address of the variable in the main memory of a change in other threads will make these thread again into the main memory copy of the latest value of this variable into the working memory, rather than continue to use the old working memory cache.

Below I have listed the use of a volatile visibility solve multi-threaded safe example:

public class VolatileDemo {// private static boolean isReady = false; private static volatile boolean isReady = false; static class ReadyThread extends Thread {public void run () {while (! isReady) {} System.out.println ( "ReadyThread finish" );}} public static void main (String [] args) throws InterruptedException {new ReadyThread () start ();. Thread.sleep (1000); // sleep 1 second thread has already started to ensure ReadyThread isReady = true;} }
after running the above code will eventually be printed in the console: ReadyThread finish, and when run after you remove the variable isReady the volatile modifier will find that the program has been run without ending, but there is no print output console .

We analyzed under this program: initially isReady is false, so the thread starts after ReadyThread started, it's because while the code block flag is false isReady will enter an infinite loop, when the modified isReady with the volatile keyword, main method where the thread after modifying the isReady is true, ReadyThread thread will be immediately informed and get the latest isReady value, followed by the while loop will end the cycle, the final print out the relevant text. When volatile modification is not used, the thread where the main method although modified isReady variable, but ReadyThread thread does not know this change, so the use of or before the old value, and therefore will always execute an infinite loop while statement.

Guess you like

Origin www.cnblogs.com/xihuankan/p/11027017.html