package com.test;
import java.util.concurrent.TimeUnit;
public class test1 {
private static boolean is = true;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while(test1.is){
i++;
//println 是synchronized 的,会强制刷新住内存的变量值到线程栈?
System.out.println("1");
/*
* sleep 会使is的值 在线程栈中失效,从新load 住内存的值?
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
}
}
}).start();
try {
TimeUnit.MICROSECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
is = false;
}
}).start();
}
}
上面这段代码,在循环中加入println 或者 sleep 都会使循环终止。为什么?