I was confused with a code snippet like this:
public class LearnTest {
private static boolean tag = true;
private static int i = 0;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while (tag) {
//System.out.println("ok");
i++;
}
}
}).start();
try {
Thread.sleep(1000);
}catch (Exception e) {
e.printStackTrace();
}
tag = false;
System.out.println(i);
}
}
In the code, we have a new-thread
and main-thread
.The result about this code will be a random value of i
and the new-thread
will not exit.Because new-thread
will not get the new tag
value.
if we change the define of tag
which will be decorated with volatile
, it will print some value of i
and new-thread
will exit. Because volatile
will keep the visibility for all thread.
But when I cannel the comment for the commentted code line,and thetag
will not be decorated witch volatile
,it will print some "ok" and exit.
why?
what I suppose is that Java's IO will do something like synchronized
, it will force the tag
side value of new-thread
to refresh from main shared memory.
This's corret?