ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JavaSE基础——关于Object类中的wait 和 notify方法

2022-01-12 22:04:50  阅读:140  来源: 互联网

标签:Thread Object list 线程 notify new JavaSE wait


1、wait 和 notify 不是线程对象的方法,而是Java中任何一个对象都有的方法。

2、wait 和 notify 方法的作用:

wait:让正在活动在该对象上的线程进入等待状态,无限期等待,直到被唤醒为止

notify:让正在该对象上等待的线程被唤醒

notifyAll():唤醒所有等待的线程

3、使用 wait 和 notify 方法,要建立在线程同步的基础上,wait 会让活动在当前对象的线程进入等待状态,并且释放占有对象的 "锁" 。

4、通过一个实例来熟悉wait 和 notify 方法

要求:使用 wait 和 notify 模拟生产者模式 和消费者模式。使用List集合模拟仓库,仓库中只能存在一个元素,有一个元素就必须进行消费,仓库为空必须进行生产,要求达到均衡,

分析:

仓库是共享的对象,生产与消费代表俩个线程,俩个线程共享仓库对象 

public class Test03 {
    public static void main(String[] args) {
        List<Object> list = new ArrayList<>();
        Thread produce = new Thread(new Produce(list));
        Thread spend = new Thread(new Spend(list));
        produce.setName("生产者线程");
        spend.setName("消费者线程");

        produce.start();

        spend.start();
    }
}

//生产线程
class Produce implements Runnable {
    //仓库,共享的对象
    private List<Object> list;

    public Produce(List<Object> list) {
        this.list = list;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (list) {
                if (!list.isEmpty()) {
                    try {
                        //表示集合不为空,该去消费.
                        //此时生产者线程遇到wait() 会释放list对象的"锁",并等待消费者线程执行
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //集合为空,应该生产
                Object data = new Object();
                list.add(data);
                System.out.println(Thread.currentThread().getName() + "生产--》" + data);
                //生产完唤醒线程,并不会释放对象 "锁",而是接着执行while循环。如果集合不为空,才会执行wait(),释放锁,让消费线程执行
                list.notify();
            }
        }
    }
}

//消费线程
class Spend implements Runnable {
    private List<Object> list;

    public Spend(List<Object> list) {
        this.list = list;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (list) {
                if (list.isEmpty()) {
                    //集合为空,该去生产
                    try {
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //集合不为空,进行消费
                Object data = list.remove(0);
                System.out.println(Thread.currentThread().getName() + "消费--》" + data);
                list.notify();
            }
        }
    }
}

 

每次消费与生产都是同一个,说明实验正确。 

标签:Thread,Object,list,线程,notify,new,JavaSE,wait
来源: https://blog.csdn.net/aetawt/article/details/122462432

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有