代码:
//保证数据的同步,只保证可见性
public class VolatileTest {
private volatile static int num=0;
public static void main(String[] args) throws InterruptedException {
new Thread(()->{
while(num==0) {
System.out.println("我是while循环");
}
}).start();
Thread.sleep(1);
num=1;
}
}