public static class SafepointTest {
public static AtomicInteger num = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {
// 以下循环 消耗 约为 26780ms
for (int i = 0; i < 1000000000; i++) {
num.getAndAdd(1);
}
System.out.println(Thread.currentThread().getName() + "执行结束!");
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
// 这里进行休眠 1s
Thread.sleep(1000);
System.out.println("num = " + num);
}
}
https://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/tip/src/share/vm/runtime/safepoint.cpp
Thread.sleep(1000);
使代码进入了 Safepoint 然后for循环并没有释放 跳出检查点Thread.sleep(1000)
其实阻塞了更长的时间 public static class SafepointTest {
public static AtomicInteger num = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
Runnable runnable = () -> {
// 以下循环 消耗 约为 26780ms
for (long i = 0; i < 1000000000; i++) {
num.getAndAdd(1);
}
System.out.println(Thread.currentThread().getName() + "执行结束!");
};
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
// 这里进行休眠 1s
Thread.sleep(1000);
System.out.println("num = " + num);
}
}
}