java 生产者和消费者demo

  1 package com.benq;
  2
  3 import java.util.*;
  4 import java.util.concurrent.TimeUnit;
  5
  6 public class HH {
  7     public static void main(String[] args){
  8
  9         var store=new MyStore<Integer>(20);
 10
 11         for(int i=0;i<10;i++){
 12             var producer=new Procuder<Integer>(store);
 13             producer.set(i);
 14             var t=new Thread(producer);
 15             t.setDaemon(true);
 16             t.start();
 17
 18             var consumer=new Consumer<Integer>(store);
 19             var t1=new Thread(consumer);
 20             t1.setDaemon(true);
 21             t1.start();
 22         }
 23
 24         try {
 25             TimeUnit.SECONDS.sleep(10);
 26         }catch (InterruptedException ie){
 27             ie.printStackTrace();
 28         }
 29     }
 30 }
 31
 32 interface IStore<T>{
 33     void produce(T t);
 34     void consume();
 35 }
 36 //仓库
 37 class MyStore<T> implements IStore<T>{
 38     //存储物品
 39     private LinkedList<T> list;
 40     //当前物品数量
 41     private volatile int num;
 42     //仓库最大存储数量
 43     private int MAX_NUM;
 44
 45     public MyStore(int maxNum) {
 46         this.list =new LinkedList<T>();
 47         this.MAX_NUM = maxNum>0?maxNum:100;
 48     }
 49
 50     @Override
 51     public void produce(T t) {
 52
 53         synchronized (list){
 54             if (num+1>this.MAX_NUM){
 55                 try {
 56                     list.wait();
 57                 }catch (InterruptedException ie){
 58                     ie.printStackTrace();
 59                 }
 60             }
 61             list.add(t);
 62             System.out.println(Thread.currentThread().getName()+" produce "+t.toString());
 63             num++;
 64             System.out.println("the store count is "+ String.valueOf(num));
 65             list.notifyAll();
 66         }
 67     }
 68
 69     @Override
 70     public void consume() {
 71         synchronized (list){
 72             if (this.num==0){
 73                 try {
 74                     list.wait();
 75                 }catch (InterruptedException ie){
 76                     ie.printStackTrace();
 77                 }
 78             }
 79             T t= list.remove();
 80             System.out.println(Thread.currentThread().getName()+" consume "+t.toString());
 81             num--;
 82             System.out.println("the store count is "+ String.valueOf(num));
 83             list.notifyAll();
 84         }
 85     }
 86 }
 87
 88 class Procuder<T> implements Runnable{
 89
 90     private MyStore<T> store;
 91     private T t;
 92
 93     public Procuder(MyStore<T> store) {
 94         this.store = store;
 95     }
 96
 97
 98     public void set(T t) {
 99         this.t=t;
100     }
101
102
103     @Override
104     public void run() {
105         if (this.store!=null){
106             store.produce(this.t);
107
108         }
109     }
110 }
111
112 class Consumer<T> implements Runnable{
113
114     private MyStore<T> store;
115
116     public Consumer(MyStore<T> store) {
117         this.store = store;
118     }
119
120     @Override
121     public void run() {
122         if (this.store!=null){
123             store.consume();
124         }
125     }
126 }

原文地址:https://www.cnblogs.com/ter-yang/p/11161330.html

时间: 2024-08-03 00:40:37

java 生产者和消费者demo的相关文章

java 生产者 与 消费者的案例

主要理解了两个问题 1.线程数据同步的问题 2.线程交替运行的方式 package ThreadDemo; /** * 生产者与消费者的案例(一,同步的问题,值的问题 二,交替执行的问题) * @author lile * 同步的问题(synchronized 知识点) * 交替执行的问题(notify ,wait, 线程等待) */public class ThreadDemo { public static void main(String[] args) { Food food = new

java 生产者与消费者问题

package concurrency; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Storage { private int capacity; private int size; public Storage(int capacity){ this.capacity=capacity; size=0; } public synchronize

java 生产者和消费者

生产者和消费者的例子 一.wait() / notify()方法 wait() / nofity()方法是基类Object的两个方法,也就意味着所有Java类都会拥有这两个方法,这样,我们就可以为任何对象实现同步机制. wait()方法:当缓冲区已满/空时,生产者/消费者线程停止自己的执行,放弃锁,使自己处于等等状态,让其他线程执行. notify()方法:当生产者/消费者向缓冲区放入/取出一个产品时,向其他等待的线程发出可执行的通知,同时放弃锁,使自己处于等待状态. 直接贴上代码 用as cl

java_Thread生产者与消费者 Demo

1 package com.bjsxt.Thread.Demo; 2 public class ProducerConsumer { 3 /** 4 * 生产者与消费者 5 * @param args 6 */ 7 public static void main(String[] args) {// 模拟线程 8 SyncStack ss = new SyncStack(); 9 Producer p = new Producer(ss); 10 Consumer c = new Consume

java 生产者与消费者初级探讨

最近学习java多线程有点迷糊,经过一天的整理,知道了什么是生产者,什么是消费者,以及消费者与生产者的关系: 在 Person类中是一个实体没有具体的对象,靠Input传入,Output读出,只有当Input有传入后,才能被Output读出,因此对呀Input和Output,要上一把同样的锁,synchronized将两个线程同步. wait(),notify(),notifyAll()都是继承至上帝类的:下面是生产者消费者的代码 //生产/消费者模式 public class Basket {

Java 生产者模式 消费者模式

1 // The standard idiom for calling the wait 2 synchronized(sharedObject) { 3 while(condition){ 4 sharedObject.wait();// Releases lock, and reacquires on wake up 5 } 6 // do action based upon condition e.g. take or put into queue 7 } 使用wait和notify函数的

JAVA 生产者和消费者Runnable实现,所有方法都放到对象里面,而非假店员之手

因为觉得把方法写到店员类里面,总是很怪异,不符合面向对象编程里面,谁的行为谁做. package com.home.nxj.ProTest; public class ProTest { public static void main(String[] args) { Clerk ck = new Clerk(); Productors p1 = new Productors(ck); Concumers c1 = new Concumers(ck); Thread t1 = new Threa

Java线程同步模型-生产者与消费者

Java生产者与消费者模型是经典Java线程同步模型,涉及使用同步锁控制生产者线程和消费者线程同步运行问题.同步对象是仓库资源,生产者线程生产向仓库中生产商品,消费者线程从仓库中消费商品,当生产者线程生产的商品达到仓库的90%时,生产者线程停止生产并通知消费者线程开始消费,当消费者线程消耗到仓库的10%时,消费者线程停止消费并通知生产者线程恢复生产,如此循环往复过程. 如下图解: T1时刻分析 T2时刻 T3时刻 T4时刻 上图的分析,T3同T1时刻相同场景,T2同T2时刻相同场景,程序如此循环

Java中的生产者、消费者问题

Java中的生产者.消费者问题描述: 生产者-消费者(producer-consumer)问题, 也称作有界缓冲区(bounded-buffer)问题, 两个进程共享一个公共的固定大小的缓冲区(仓库). 其中一个是生产者, 用于将产品放入仓库: 另外一个是消费者, 用于从仓库中取出产品消费. 问题出现在当仓库已经满了, 而此时生产者还想向其中放入一个新的产品的情形, 其解决方法是让生产者此时进行等待, 等待消费者从仓库中取走了一个或者多个产品后再去唤醒它. 同样地, 当仓库已经空了, 而消费者还