登录
首页 >  文章 >  java教程

JavanotifyAll唤醒线程全解析

时间:2025-10-09 11:27:26 274浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Java中notifyAll唤醒线程方法详解》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

notifyAll()用于唤醒所有等待特定对象监视器的线程,需在synchronized块中调用,配合wait()实现线程协作,如生产者-消费者模型中通过notifyAll()确保多个消费者或生产者被唤醒,避免线程阻塞。

如何在Java中使用notifyAll唤醒线程

在Java中,notifyAll() 用于唤醒所有正在等待特定对象监视器的线程。它通常与 synchronized 块、wait() 方法配合使用,实现线程间的协作。下面介绍如何正确使用 notifyAll() 来唤醒线程。

1. 理解 wait() 和 notifyAll() 的工作机制

这三个方法都定义在 Object 类中,必须在 synchronized 上下文中调用:

  • wait():使当前线程释放锁并进入等待状态,直到被唤醒。
  • notify():随机唤醒一个等待该对象锁的线程。
  • notifyAll():唤醒所有等待该对象锁的线程,这些线程将重新竞争获取锁。
注意:直接调用这些方法而不加 synchronized 会抛出 IllegalMonitorStateException。

2. 使用步骤和代码示例

以下是一个生产者-消费者模型的简单示例,展示如何用 notifyAll() 正确唤醒多个等待线程:

import java.util.LinkedList;
import java.util.Queue;

public class NotifyAllExample {
    private final Queue<String> queue = new LinkedList<>();
    private final int MAX_SIZE = 3;

    public void produce(String item) throws InterruptedException {
        synchronized (this) {
            while (queue.size() == MAX_SIZE) {
                System.out.println("队列已满,生产者等待...");
                this.wait(); // 释放锁并等待
            }
            queue.add(item);
            System.out.println("生产了: " + item);
            this.notifyAll(); // 唤醒所有等待线程(包括消费者)
        }
    }

    public String consume() throws InterruptedException {
        synchronized (this) {
            while (queue.isEmpty()) {
                System.out.println("队列为空,消费者等待...");
                this.wait(); // 释放锁并等待
            }
            String item = queue.poll();
            System.out.println("消费了: " + item);
            this.notifyAll(); // 唤醒所有等待线程(包括生产者)
            return item;
        }
    }
}

3. 多线程测试 notifyAll 的效果

启动多个生产者和消费者线程,观察 notifyAll 如何唤醒多个等待线程:

public class TestNotifyAll {
    public static void main(String[] args) {
        NotifyAllExample example = new NotifyAllExample();

        // 启动多个消费者线程
        for (int i = 1; i <= 3; i++) {
            new Thread(() -> {
                try {
                    while (true) {
                        example.consume();
                        Thread.sleep(2000); // 模拟处理时间
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }, "Consumer-" + i).start();
        }

        // 启动生产者线程
        new Thread(() -> {
            int counter = 1;
            try {
                while (true) {
                    example.produce("item-" + counter++);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, "Producer").start();
    }
}

4. 关键注意事项

使用 notifyAll() 时需注意以下几点:

  • 始终在 synchronized 块或方法中调用 wait() 和 notifyAll()。
  • 使用 while 而不是 if 判断等待条件,防止虚假唤醒或条件变化后继续执行。
  • notifyAll() 会唤醒所有等待线程,但它们需要重新竞争锁,只有一个能进入同步块。
  • 适合场景:当多个线程可能等待同一条件时(如多个消费者),notifyAll() 更安全。

基本上就这些。notifyAll() 是实现线程协作的重要工具,合理使用可避免死锁和线程饥饿问题。

到这里,我们也就讲完了《JavanotifyAll唤醒线程全解析》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>