学习LockSupport类中通过part()和unpart()方法来实现阻塞和唤醒线程
1、使用part()和unpart()线程等待和唤醒情况,且无锁块要求
public static void main(String[] args) {
Thread parkThread = new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "\t" + "parkThread come in");
LockSupport.park();
System.out.println(Thread.currentThread().getName() + "\t" + "parkThread out");
}, "parkThread");
parkThread.start();
try{TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();} // 延迟3秒
Thread unparkThread = new Thread(() -> {
LockSupport.unpark(parkThread);
System.out.println(Thread.currentThread().getName() + "\t" + "unparkThread unpark");
}, "unparkThread");
unparkThread.start();
}
<