notify 和 wait 实现生产者消费者模型

  1. 对于wait()和notify()的理解
    void notify()
    唤醒在此对象监视器上等待的单个线程
    void notifyAll()
    唤醒在此对象监视器上等待的所有线程
    void wait( )
    导致当前的线程等待,直到其他线程调用此对象的notify( ) 方法或 notifyAll( ) 方法
    void wait(long timeout)
    是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁,以便其他正在等待此锁的线程可以得到同步锁并运行(释放锁,阻塞当前线程)
    void wait(long timeout, int nanos)
    导致当前的线程等待,直到其他线程调用此对象的notify( ) 方法或 notifyAll( ) 方法,或者其他线程打断了当前线程,或者指定的时间过完。
    以上方法调用,主要是对加锁资源的竞争,synchronized 否则会报IllegalMonitorStateException 异常
  2. 生产者消费者
public class Threads1 {
    public static void main(String[] args) {
        Threads1 consumer = new Threads1();
        consumer.print();
    }

    class Res{
        String userName;
        String sex;
        // false  ----InputThread 赋值  唤醒 OuputThread
        //true -- OuputThread赋值  唤醒 InputThread
        public boolean flag = false;
    }

    class InputThread extends Thread{
        private Res res;

        public InputThread(Res res){
            this.res = res;
        }

        @Override
        public void run() {
            while (true){
                synchronized (res){
                    //false  则进行当前线程   否则进行阻塞
                    if (res.flag) {
                        try {
                            //释放当前锁,同时进行阻塞
                            res.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    res.userName = "王孝峰的女朋友";
                    res.sex = "女";
                    System.out.println(res.toString());
                    res.flag = true;
                    res.notify();
                }
            }
        }
    }

    class OuputThread extends Thread{
        private Res res;

        public OuputThread(Res res){
            this.res = res;
        }
        @Override
        public void run() {
            while (true){
                synchronized (res){
                    if(!res.flag){
                        try {
                            //释放当前锁,同时进行阻塞
                            res.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    res.userName = "王孝峰";
                    res.sex = "男";
                    System.out.println(res.toString());
                    res.flag = true;
                    res.notify();
                }
            }
        }
    }

    public void print() {
        // 全局对象
        Res res = new Res();
        // 输入线程
        InputThread inputThread = new InputThread(res);
        OuputThread ouputThread = new OuputThread(res);
        inputThread.start();
        ouputThread.start();
    }

}

主要思想就是
输入线程输入参数之后唤醒输出线程,同时自己进行wait
输出线程输入参数之后唤醒输入线程,同时自己进行wait

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值