public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {
Unsafe unsafe = UnsafeUtil.getUnsafe();
Thread currThread = Thread.currentThread();
new Thread(()->{
try {
Thread.sleep(3000);
currThread.interrupt();
//unsafe.unpark(currThread);
} catch (Exception e) {
System.out.println(e);
}
}).start();
unsafe.park(false, 0);
//currThread.sleep(5000);
System.out.println("SUCCESS!!!");
}
结果:程序不会报错,三秒后打印 SUCCESS!!!
如果把 unsafe.park(false, 0);换成currThread.sleep(5000);则会报线程中断异常。
我知道Thread.interrupt会对阻塞线程抛出InterruptedException异常。
但是park不算阻塞么?其实我一直对Object.wait()和Unsafe.park()方法的区别不太理解。
还请大神们帮忙解释一下。