62,635
社区成员




public class Test {
public static boolean stop;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
int i=0;
while (!stop)
i++;
});
System.out.println("1");
TimeUnit.SECONDS.sleep(1);
System.out.println("1");
stop=true;
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
int i=0;
while (!stop)
i++;
});
thread.start();
System.out.println("1");
TimeUnit.SECONDS.sleep(1);
System.out.println("1");
stop=true;
}
public static boolean stop;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
long i=0;
while (!stop) {
i++;
if(i==10000000000L){
}
}
System.out.println("end"+i);
});
thread.start();
System.out.println("1");
TimeUnit.SECONDS.sleep(1);
System.out.println("1");
stop=true;
}
抛个砖引下玉
(1)按上面代码,stop又变成可见了
(2)楼主的源代码,指定启动参数-Xint 解释模式运行,stop也可见了