登录
首页 >  文章 >  java教程

JavanotifyAll唤醒线程全解析

时间:2025-10-05 09:40:35 453浏览 收藏

本文深入解析Java中`notifyAll()`方法,该方法用于唤醒所有等待特定对象监视器的线程,是实现线程间协作的关键。文章强调了`notifyAll()`必须在`synchronized`块中配合`wait()`方法使用,以避免`IllegalMonitorStateException`。通过生产者-消费者模型的示例,详细展示了如何利用`notifyAll()`确保多个消费者或生产者线程被唤醒,有效防止线程阻塞。此外,还提供了多线程测试用例,帮助开发者理解`notifyAll()`在实际应用中的效果,以及使用时的关键注意事项,如使用`while`循环判断等待条件以应对虚假唤醒。掌握`notifyAll()`的正确使用,对于构建高效、稳定的多线程Java应用至关重要。

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学习网公众号!

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