码迷,mamicode.com
首页 > 编程语言 > 详细

java 生产者和消费者demo

时间:2019-07-10 01:18:29      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:notifyall   set   add   tac   stat   int   util   static   ack   

  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 }

 

java 生产者和消费者demo

标签:notifyall   set   add   tac   stat   int   util   static   ack   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!