更新时间:2023-10-27 来源:黑马程序员 浏览量:
notify()和notifyAll()都是Java中用于多线程编程的方法,用于在多线程环境中管理线程的等待和唤醒操作。它们的主要区别在于唤醒的目标线程数以及线程等待的条件。
notify()方法用于唤醒等待在对象上的一个随机线程。如果多个线程在同一个对象上等待,那么只有其中的一个线程会被唤醒,但无法确定是哪一个线程。这个方法通常用于线程之间的竞争条件,其中只有一个线程能够获得资源的情况。
示例代码:
class SharedResource { public synchronized void doSomething() { System.out.println("Thread " + Thread.currentThread().getId() + " is working."); notify(); // 唤醒等待的线程 } } public class NotifyExample { public static void main(String[] args) { SharedResource resource = new SharedResource(); Runnable task = () -> { synchronized (resource) { try { System.out.println("Thread " + Thread.currentThread().getId() + " is waiting."); resource.wait(); // 等待资源 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + Thread.currentThread().getId() + " is awake."); } }; Thread thread1 = new Thread(task); Thread thread2 = new Thread(task); thread1.start(); thread2.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } resource.doSomething(); // 唤醒一个等待的线程 } }
在上面的示例中,notify()唤醒了其中一个等待的线程,但不能确定是哪一个线程被唤醒。
notifyAll()方法用于唤醒等待在对象上的所有线程。这意味着所有等待的线程都有机会争夺资源。这通常用于广播消息或者在共享资源可用时唤醒所有等待线程的情况。
示例代码:
class SharedResource { public synchronized void doSomething() { System.out.println("Thread " + Thread.currentThread().getId() + " is working."); notifyAll(); // 唤醒所有等待的线程 } } public class NotifyAllExample { public static void main(String[] args) { SharedResource resource = new SharedResource(); Runnable task = () -> { synchronized (resource) { try { System.out.println("Thread " + Thread.currentThread().getId() + " is waiting."); resource.wait(); // 等待资源 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread " + Thread.currentThread().getId() + " is awake."); } }; Thread thread1 = new Thread(task); Thread thread2 = new Thread(task); thread1.start(); thread2.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } resource.doSomething(); // 唤醒所有等待的线程 } }
在上面的示例中,notifyAll()唤醒了所有等待的线程,它们都有机会争夺资源。
总结:
(1)notify()唤醒一个等待的线程,选择唤醒哪个线程不确定。
(2)notifyAll()唤醒所有等待的线程,它们都有机会争夺资源。
【AI设计】北京143期毕业仅36天,全员拿下高薪offer!黑马AI设计连续6期100%高薪就业
2025-09-19【跨境电商运营】深圳跨境电商运营毕业22个工作日,就业率91%+,最高薪资达13500元
2025-09-19【AI运维】郑州运维1期就业班,毕业14个工作日,班级93%同学已拿到Offer, 一线均薪资 1W+
2025-09-19【AI鸿蒙开发】上海校区AI鸿蒙开发4期5期,距离毕业21天,就业率91%,平均薪资14046元
2025-09-19【AI大模型开发-Python】毕业33个工作日,就业率已达到94.55%,班均薪资20763元
2025-09-19【AI智能应用开发-Java】毕业5个工作日就业率98.18%,最高薪资 17.5k*13薪,全班平均薪资9244元
2025-09-19