We know about multithreading, Synchorized,sleep and Valatile, but when the three nouns and "Java variable visibility" of the topic linked together do not know if you can still keep the brain clear???
Recently saw a question about the visibility of Java variables, feel more fresh to check some information, the following share to you:
First of all, let's look at a multithreaded code of execution:
Package Com.test;import Java.util.concurrent.TimeUnit; public class Test1 {private static Boolean is = true; public static void Main (string[] args) {new Runnable () {@Override public void Ru N () {int i = 0; while (test1.is) {i++; 1//synchronized (this) {} forces the variable value of the main memory to be flushed to the thread stack? 2//system.out.println ("1"); PRINTLN is synchronized, forcing the variable value of the main memory to be flushed to the thread stack? 3//sleep will the value of the main memory from the new load? try {//TimeUnit.MICROSECONDS.sleep (1); }catch (interruptedexception e) {//E.printstacktrace (); }}}). Start (); try {TimeUnit.SECONDS.sleep (1); } catch (Interruptedexception e) {e.printstacktrace (); } New Thread (new Runnable () {@OVerride public void Run () {is = false; Set is false to cause the above thread to end the while loop}). Start (); }}when we run the current code, we find that the program works, but if we uncomment any line in the lines (1-3) and the program terminates, we have a question: why? Synchronized will force the value of the main memory to be flushed to the thread stack?? So what does sleep do?
Here's a solution to the problem that occurs during code execution:
Q1: Why does the program not terminate after commenting the code?
A1: Because the value of a Boolean is=true variable is loaded into its own working memory by a previous thread (called Thread A), it does not necessarily write to main memory immediately after the subsequent thread (thread B) Changes the Boolean is=false (though the problem should be written to main memory immediately, Because the thread is about to exit after the thread finishes executing the Is=false, thread A does not have to load into working memory immediately after it has been written to it, so the program never terminates. This is what most of us think of, but in fact the JVM for the current hardware level has done a great degree of optimization, basically to a large extent to ensure that the working memory and main memory of the timely synchronization, equivalent to the default use of volatile. But only to the maximum extent! When the CPU resource is always occupied, the synchronization between the working memory and the main memory, that is, the visibility of the variable will not be so timely! The conclusions are verified later.
Q2: Why does the cancellation of any one of the code block (A/a) program terminate??
A2: line number for the code has one thing in common: all involved in the synchronized synchronous lock, then whether it will be like a small series in the beginning of the question "synchronized will force refresh the main memory of the variable value to the thread stack"? Or is it that the sleep method refreshes the variable value of main memory to line stacks?
In fact, we all know that synchronized only guarantees the variability of the variables in the synchronization block, and the is variable is not in that synchronization block, so obviously this is not the result; then we can try to add the following code after the code "i++":
for (int k=0;k<100000;k++) { new Object ();}
At this point, we re-run the program, the program will be terminated immediately, which is why?
because we know that when the CPU is occupied, the visibility of the data is not very well guaranteed. As in the example above, the program will loop through the i++ operation until the code is added, so the CPU will be occupied by the operation, and for a large number of new Object operations, the CPU is not the main station time operation. The real time-consuming should be in the memory allocation (because the CPU processing speed significantly faster than the memory, otherwise there will be no CPU registers), so the CPU idle will follow the JVM optimization benchmark, as soon as possible to ensure the visibility of the data, from main memory to synchronize is variable to the working memory, The final result is the end of the program, which is the sleep () method, although it does not involve synchronous operation, it can still cause the program to terminate, because the sleep () method frees the CPU, but does not release the lock;
PostScript ? Knowledge Expansion:
Volatile: This keyword guarantees the visibility of variable threads, all of which have access to variables modified by volatile, must be read from main memory, and write back to memory immediately after the modification of the working RAM, to ensure the visibility of other threads, and the same effect of the keyword is final.
Synchronized: All synchronization operations must be guaranteed 1, atomicity 2, visibility, so the changes that occur in the synchronization block are immediately written back to main memory
Sleep: This method only yields the CPU execution time and does not release the lock;
? message
technology lies in the continuous discovery and summary, we need to put their own knowledge into the collation of output, so as to the knowledge of the input better understanding; the above is purely personal understanding, if there are errors, please correct them;
Amusing problems with visibility of Java variables