java线程通讯-生产者消费者模型(管道法)

多线程-线程通讯-生产者消费者模型

多线程的使用往往不单单是单个线程的实现,而是多个线程的相互协作实现逻辑业务。

什么是线程通讯?
多个线程协作完成共享资源的使用,实现线程安全,线程不死锁。

线程通讯实现方式:等待(wait()) | 通知 notify() .notifyall()
wait()、notify()、notifyall() 方法都是Object类下的方法。

wait() : 让当前线程进入等到状态,并且释放锁资源。
notify() : 唤醒一个等待线程,且随机线程。
notifyall() : 唤醒所有等待线程。

生产者消费者模型 – 管道法

生产者消费者是一种现场和场景,并不是一种模式。管道法: 类似于缓存机制,生产者将生产的食物放置容器中,消费者从容器中取食物。

案例:
生产者: 生产食物。
消费者:消费食物。
食物容器:食物不足10个的时候需要生产者生产,且放入食物到容器中,消费者从容器中取食物,没有食物时,只能等待生产者生产食物。
在这里插入图片描述
实现:

  // 食物实体类
   static class Food{
   }
// 食物容器类(公共资源)
static class FoodContainer{
       int count = 0;
       // 定义一个容器大小为10
       Food[] foods = new Food[10];

       // 往容器放食物
   public synchronized void  push(Food food){
       // 如果容器满了,等待,通知消费者消费
       if(count==foods.length){
           try {
               wait();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       // 没有满,生产食物,放到容器中。
       foods[count] = food;
       count++;
       // 通知消费者消费
       notifyAll();
   }
   
   // 从容器中取食物
    public synchronized  void Take(){
       if(count<=0){
           try {
               wait();
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
       }
       // 有食物则取食物
        count--;
       // 通知生产者生产
        notifyAll();
    }

    }
  // 生产者
  static class ProductMan implements Runnable{
      // 定义容器
      FoodContainer foodContainer;
      public ProductMan(FoodContainer foodContainer){
          this.foodContainer = foodContainer;
      }


      // 生产者生产食物
      public void run() {
          //生产食物
          for (int i = 0; i <20 ; i++) {
              foodContainer.push(new Food());
              System.out.println(Thread.currentThread().getName()+": 生产了"+i+"只鸡");
          }
      }
  }
  // 消费者
  static class Consumman implements Runnable{
      // 定义容器
      FoodContainer foodContainer;
      public Consumman(FoodContainer foodContainer){
          this.foodContainer = foodContainer;
      }

        // 消费者消费食物
        public void run() {
            //消费食物
            for (int i = 0; i <20 ; i++) {
                System.out.println(Thread.currentThread().getName()+": 消费了"+i+"只鸡");
                foodContainer.Take();
            }
        }
    }
    public static void main(String[] args) {
         // 启动生产者和消费者线程
         FoodContainer foodContainer =new FoodContainer();
         new Thread(new ProductMan(foodContainer)).start();
         new Thread(new Consumman(foodContainer)).start();
    }

结果:生产者生产到第10只鸡的时,告诉消费者来消费才继续生产。
在这里插入图片描述
注意: 容器属于公共资源,所以在对容器进行操作事一定要加上线程同步。
下一篇介绍 生产者与消费者的另一种实现方式 信号灯法

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Survivor001

你可以相信我,如果你愿意的话

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值