wait方法
/** * wait的使用 */ public class WaitDemo1 { public static void main(String[] args) { Object lock = new Object(); Thread t1 = new Thread(() -> { System.out.println("线程1开始执行"); try { synchronized (lock) { System.out.println("线程1调用wait方法...."); // 无限期的等待状态 lock.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程1执行完成"); }, "线程1"); t1.start(); } }
有参wait线程和无参wait线程
/** * 有参wait线程和无参wait线程 */ public class WaitDemo2 { public static void main(String[] args) { Object lock1 = new Object(); Object lock2 = new Object(); Thread t1 = new Thread(()->{ System.out.println("线程1开始执行"); synchronized (lock1){ try { lock1.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程1执行完成"); } },"无参wait线程"); t1.start(); Thread t2 = new Thread(()->{ System.out.println("线程2开始执行"); synchronized (lock2){ try { lock2.wait(60*60*1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程2执行完成"); } },"有参wait线程"); t2.start(); } }
①其他线程调用该对象的 notify 方法.
②wait 等待时间超时 (wait 方法提供一个带有 timeout 参数的版本, 来指定等待时间).
③其他线程调用该等待线程的 interrupted 方法, 导致 wait 抛出 InterruptedException 异常
notify 方法只是唤醒某一个等待的线程
notify方法的使用
/** * wait的使用, 如果有多个线程等待,随机挑选一个wait状态的线程 */ public class WaitNotifyDemo { public static void main(String[] args) { Object lock1 = new Object(); Object lock2 = new Object(); Thread t1 = new Thread(()->{ System.out.println("线程1开始执行"); try { synchronized (lock1) { System.out.println("线程1调用wait方法"); lock1.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程1执行完成"); },"线程1"); Thread t2 = new Thread(()->{ System.out.println("线程2开始执行"); try { synchronized (lock1) { System.out.println("线程2调用wait方法"); lock1.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程2执行完成"); },"线程2"); t1.start(); t2.start(); // 唤醒 lock1 对象上休眠的线程的(随机唤醒一个) Thread t3 = new Thread(()->{ try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程3开始执行"); synchronized (lock1){ //发出唤醒通知 System.out.println("执行了唤醒"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } },"线程3"); t3.start(); } }
notifyAll方法可以一次唤醒所有的等待线程
notifyAll方法的使用
/** * notifyAll-唤醒所有线程 */ public class WaitNotifyAll { public static void main(String[] args) { Object lock = new Object(); new Thread(() -> { System.out.println("线程1:开始执行"); synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程1:执行完成"); } }, "无参wait线程").start(); new Thread(() -> { synchronized (lock) { System.out.println("线程2:开始执行 |" + LocalDateTime.now()); try { lock.wait(60 * 60 * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程2:执行完成 | " + LocalDateTime.now()); } }, "有参wait线程").start(); new Thread(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { System.out.println("唤醒所有线程"); lock.notifyAll(); } }).start(); } }
notify和notifyAll方法的区别