线程唤醒的两种方式

时间:2025-01-24 08:03:10
public static void main(String[] args) { Thread t0 = new Thread(() -> { Thread current = Thread.currentThread(); System.out.println("{},开始执行!"+current.getName()); for(;;){//spin 自旋 System.out.println("准备park住当前线程:{}...."+current.getName()); LockSupport.park(); System.out.println("当前线程{}已经被唤醒...."+current.getName()); } },"t0"); t0.start(); try { Thread.sleep(2000); System.out.println("准备唤醒{}线程!"+t0.getName()); LockSupport.unpark(t0); Thread.sleep(2000); t0.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } }