线程篇-wait与notify的应用

想实现wait与notify的互斥,使他们在同步锁当中,这是一种实现互斥的一种
解决方案

下面使用生产者与消费者来演示wait与notify的应用

代码示例:


工厂类
package com.hzh.shopping;

import java.util.LinkedList;

/*
* 商品工厂类
* */
public class ProductFactory {
    LinkedList<String> list = new LinkedList<>();
    int max = 10;

    //存储商品

    public void SaveProduct(){

    }

    //生产商品
    public synchronized void ProduceProduct(String name){
        //生产到了上限,停止生产
        while (list.size()==max){
            try {
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        list.add(name);
        System.out.println(Thread.currentThread().getName()+"生产了"+name+",当前商品总数"+list.size());

        //仓库满了,通知消费者消费
        if (list.size()==max){
            try {
                this.notifyAll();//唤醒所有的线程,唤醒一个的话会陷入等待状态,导致无法生产
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //消费商品

    public synchronized void ConsumeProduct(){
        //如果商品为空,则进行等待
        while (list.size()==0){
            try {
                this.wait();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        String name = list.remove(0);
        System.out.println(Thread.currentThread().getName()+"消费了"+name+",当前商品总数"+list.size());

        if (list.size()==0){
            this.notifyAll();
        }
    }




}

生产者
package com.hzh.shopping;

/*
* 生产者
* */
public class ProcedureRunnable implements Runnable{
    private ProductFactory factory;

    public ProcedureRunnable() {
    }

    public ProcedureRunnable(ProductFactory factory) {
        this.factory = factory;
    }

    public ProductFactory getFactory() {
        return factory;
    }

    public void setFactory(ProductFactory factory) {
        this.factory = factory;
    }

    @Override
    public void run() {
        int i=0;
        while (true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            factory.ProduceProduct("商品"+i);
            i++;
        }
    }
}

消费者
package com.hzh.shopping;

/*
* 消费者
* */
public class ConsumerRunnable implements Runnable{
    private ProductFactory factory;

    public ConsumerRunnable() {

    }

    public ConsumerRunnable(ProductFactory factory) {
        this.factory = factory;
    }

    public ProductFactory getFactory() {
        return factory;
    }

    public void setFactory(ProductFactory factory) {
        this.factory = factory;
    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            factory.ConsumeProduct();
        }
    }
}

测试类
package com.hzh.shopping;

/*
* 测试类
* */
public class Test {
    public static void main(String[] args) {
        ProductFactory factory = new ProductFactory();
        //十个生产者

        ProcedureRunnable runnable1 = new ProcedureRunnable(factory);

        for (int i = 0; i < 10; i++) {
            new Thread(runnable1,"生产者"+i).start();
        }

        //二十个消费者

        ConsumerRunnable runnable2 = new ConsumerRunnable(factory);

        for (int i = 0; i < 20; i++) {
            new Thread(runnable2,"消费者"+i).start();
        }


    }
}

上面的方案很好的解释了wait与notify的应用,当仓库的容量已经满了,这个
时候唤醒沉睡的消费者线程,并且让生产者线程进入wait状态
当仓库的容量为0时,唤醒生产者线程,并且将消费者线程进入wait状态
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值