Java并发编程中的生产者与消费者模型简述

作者:低调小一 时间:2023-02-16 20:33:18 

概述
对于多线程程序来说,生产者和消费者模型是非常经典的模型。更加准确的说,应该叫“生产者-消费者-仓库模型”。离开了仓库,生产者、消费者就缺少了共用的存储空间,也就不存在并非协作的问题了。

示例
定义一个场景。一个仓库只允许存放10件商品,生产者每次可以向其中放入一个商品,消费者可以每次从其中取出一个商品。同时,需要注意以下4点:
1.  同一时间内只能有一个生产者生产,生产方法需要加锁synchronized。
2.  同一时间内只能有一个消费者消费,消费方法需要加锁synchronized。
3.  仓库为空时,消费者不能继续消费。消费者消费前需要循环判断当前仓库状态是否为空,空的话则消费线程需要wait,释放锁允许其他同步方法执行。
4.  仓库为满时,生产者不能继续生产,生产者生产钱需要循环判断当前仓库状态是否为满,满的话则生产线程需要wait,释放锁允许其他同步方法执行。

示例代码如下:

  


public class Concurrence {
   public static void main(String[] args) {
     WareHouse wareHouse = new WareHouse();
     Producer producer = new Producer(wareHouse);
     Consumer consumer = new Consumer(wareHouse);

new Thread(producer).start();
     new Thread(consumer).start();
   }
 }

class WareHouse {
   private static final int STORE_SIZE = 10;
   private String[] storeProducts = new String[STORE_SIZE];
   private int index = 0;

public void pushProduct(String product) {
     synchronized (this) {
       while (index == STORE_SIZE) {
         try {
           this.wait();
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }

storeProducts[index++] = product;
       this.notify();

System.out.println("生产了: " + product + " , 目前仓库里共: " + index
           + " 个货物");
     }
   }

public synchronized String getProduct() {
     synchronized (this) {
       while (index == 0) {
         try {
           this.wait();
         } catch (InterruptedException e) {
           e.printStackTrace();
         }
       }

String product = storeProducts[index - 1];
       index--;
       System.out.println("消费了: " + product + ", 目前仓库里共: " + index
           + " 个货物");
       this.notify();
       return product;
     }
   }
 }

class Producer implements Runnable {
   WareHouse wareHouse;

public Producer(WareHouse wh) {
     this.wareHouse = wh;
   }

@Override
   public void run() {
     for (int i = 0; i < 40; i++) {
       String product = "product" + i;
       this.wareHouse.pushProduct(product);
     }
   }
 }

class Consumer implements Runnable {
   WareHouse wareHouse;

public Consumer(WareHouse wh) {
     this.wareHouse = wh;
   }

@Override
   public void run() {
     for (int i = 0; i < 40; i++) {
       this.wareHouse.getProduct();
     }
   }
 }
标签:Java,并发
0
投稿

猜你喜欢

  • Flutter WebView 预加载实现方法(Http Server)

    2023-06-25 23:14:35
  • C# Record构造函数的行为更改详解

    2022-01-21 04:50:11
  • 基于Apache组件分析对象池原理的实现案例分析

    2023-11-05 16:55:57
  • C#中的Explicit和Implicit详情

    2022-09-06 06:50:54
  • c#网络唤醒功能实现

    2022-07-03 03:26:51
  • Java maven详细介绍

    2022-10-12 06:45:31
  • Java实现走迷宫回溯算法

    2022-06-02 05:11:29
  • aop的实现原理_动力节点Java学院整理

    2022-04-02 16:09:21
  • Java Redis Redisson配置教程详解

    2022-10-13 06:32:39
  • mybatis使用pagehelper插件过程详解

    2023-02-16 18:11:11
  • SpringBoot中多环境配置和@Profile注解示例详解

    2023-11-29 05:39:04
  • C#制作鹰眼的详细全过程(带注释)实例代码

    2022-03-01 06:56:12
  • Java 转型(向上或向下转型)详解及简单实例

    2021-10-17 14:29:27
  • C#生成带二维码的专属微信公众号推广海报实例代码

    2023-04-04 23:30:57
  • 详解springboot项目带Tomcat和不带Tomcat的两种打包方式

    2023-11-28 08:23:41
  • 通俗易通讲解Android蓝牙键值适配

    2022-06-19 07:50:34
  • Javaweb实现在线人数统计代码实例

    2023-01-17 10:19:24
  • Java:泛型知识知多少

    2023-11-24 23:08:44
  • 详解Android中Service AIDL的使用

    2022-10-02 05:08:16
  • jdk15的安装与配置全过程记录

    2023-01-06 05:45:10
  • asp之家 软件编程 m.aspxhome.com