标签:static inter volatile public read dex interrupt 停止 int
volatile无法破除循环
public class my {
private volatile static int num=0;
public static void main(String[]args) throws InterruptedException
{
new Thread(()->{
while(num==0)
{
}
}).start();
Thread.sleep(1000);
num=1; //理论上1秒后停止,因为死循环没有办法同步num
}
}
修改后:
public class my {
private volatile static int num=0;
public static void main(String[]args) throws InterruptedException
{
new Thread(()->{
while(num==0)
{
}
}).start();
Thread.sleep(1000);
num=1; //理论上1秒后停止,因为死循环没有办法同步num
}
标签:static inter volatile public read dex interrupt 停止 int
原文地址:https://blog.51cto.com/14437184/2430515