Perbezaan asas: tidur ialah kaedah dalam kelas Thread dan akan tidak boleh dimasukkan serta-merta Dalam keadaan berjalan, tunggu ialah kaedah dalam kelas Objek Setelah objek memanggil kaedah tunggu, kaedah notify() dan notifyAll() mesti digunakan untuk membangkitkan proses
/** * 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线程 */ 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(); } }
/** * 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(); } }
kaedah notifyAll boleh bangun sekaligus Utas menunggu
Penggunaan kaedah 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(); } }
Perbezaan antara kaedah notify dan notifyAll
Atas ialah kandungan terperinci Apakah perbezaan antara kaedah tidur dan menunggu di Jawa?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!