notify和notifyAll

本文通过包子铺老板与顾客的生动例子,解析了Java中的notify和notifyAll方法在多线程同步中的区别,展示了notify唤醒单个等待线程,而notifyAll唤醒所有等待线程的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

notify:
notify只会唤醒众多等待线程中的其中一个线程
在这里插入图片描述
用包子铺案例来演示一下

package com.thread;

/**
 * @author 邓亚非
 */
public class TestNotify {
    private static Object object=new Object();
    public static void main(String[] args) {
        //        顾客1线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    System.out.println("顾客1告诉老板要什么包子");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("包子已经做好了,顾客1可以去买了");
                }
            }
        }).start();
        //        顾客2线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    System.out.println("顾客2告诉老板要什么包子");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("包子已经做好了,顾客2可以去买了");
                }
            }
        }).start();
//        老板线程
        new Thread(new Runnable() {
            @Override
            public void run() {
//                5秒钟做包子
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (object){
                    System.out.println("告诉顾客包子已经做好了");
                    object.notify();
                }
            }
        }).start();
    }
}

测试:
在这里插入图片描述
notifyAll()方法会唤醒所有语句在等待的线程
在这里插入图片描述
我们再来模拟一下
代码:

package com.thread;

/**
 * @author 邓亚非
 */
public class TestNotify {
    private static Object object=new Object();
    public static void main(String[] args) {
        //        顾客1线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    System.out.println("顾客1告诉老板要什么包子");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("包子已经做好了,顾客1可以去买了");
                }
            }
        }).start();
        //        顾客2线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object){
                    System.out.println("顾客2告诉老板要什么包子");
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("包子已经做好了,顾客2可以去买了");
                }
            }
        }).start();
//        老板线程
        new Thread(new Runnable() {
            @Override
            public void run() {
//                5秒钟做包子
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (object){
                    System.out.println("告诉顾客包子已经做好了");
                    object.notifyAll();
                }
            }
        }).start();
    }
}

测试:
在这里插入图片描述通过案例你们应该明白了吧哈哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值