public static void main(String[] args){ doSync(); } private static void doSync() { MyThread myThread = new MyThread(); new Thread(myThread).start(); Thread tt = new Thread(myThread); tt.start(); try { Thread.sleep(1000, 0); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("try interuppt "); tt.interrupt(); } class MyThread implements Runnable{ private ReentrantLock lock = new ReentrantLock(); @Override public void run() { try { System.out.println("try to wait"); lock.lockInterruptibly(); Thread.sleep(5000); // this.wait(); System.out.println("after wait"); lock.unlock(); } catch (InterruptedException e) { System.out.println("recv interrupt"); } } }
输出:
try to wait try to wait try interuppt recv interrupt after wait
时间: 2024-10-14 18:04:25