也学了很久 java 了,但对重排序那块还是不太理解。直接上代码吧
public class App {
private static boolean flag = false;
private static int cnt = 0;
public static void main(String[] argv) throws Exception{
new Thread(() -> {
while (!flag) {
try {
// Thread.sleep(2000L);
} catch (Exception e) {
e.printStackTrace();
}
// System.out.println(cnt);
}
System.out.println("----a end");
}).start();
Thread.sleep(1000L);
new Thread(() -> {
refresh();
}).start();
}
public static void refresh() {
System.out.println("--stat");
flag = true;
cnt = 3;
System.out.println("---end");
}
}
执行这个代码,必然是不可以看到----a end这条输出的,但我们把上面的注释代码,随便取消注释 1 个,就可以看到输出----a end,然后程序结束。
有大佬可以简单讲下经验吗