zoukankan      html  css  js  c++  java
  • 生产者消费者

    模拟 生产者生产产品  放到天猫    消费者去天猫消费

    Tmail

    public class Tmail {
    
        private  int count;
    
        public  final int MAX_COUNT = 10;
    
        public synchronized void push(){
            while (count >= MAX_COUNT) {
                try {
                    System.out.println(Thread.currentThread().getName() + "库存达到上线,生产者stop");
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
                count++;
                System.out.println(Thread.currentThread().getName()+"生产者生产,当前库存为:"+count);
                notifyAll();
    
        }
        public synchronized void take(){
            while (count <= 0){
                try {
                    System.out.println(Thread.currentThread().getName()+"库存数量为零,消费者等待");
                    wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            count--;
            System.out.println(Thread.currentThread().getName()+"消费者消费,当前库存为:"+count);
            notifyAll();
        }
    
    }
    

      PushTarget

    public class PushTarget implements  Runnable {
    
         private Tmail tmail;
    
         public PushTarget(Tmail tmail){
             this.tmail=tmail;
         }
    
        public void run() {
            while (true) {
                tmail.push();
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
    
            }
        }
    }
    

      TakeTarget

    public class TakeTarget implements  Runnable {
       private  Tmail tmail;
       public  TakeTarget(Tmail tmail){
           this.tmail = tmail;
       }
        public void run() {
            while (true) {
                tmail.take();
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    
    }
    

      Test

    public class Test {

    public static void main(String[] args){
    Tmail tmail = new Tmail();
    PushTarget p = new PushTarget(tmail);
    TakeTarget t = new TakeTarget(tmail);

    new Thread(p).start();
    new Thread(p).start();
    new Thread(p).start();
    new Thread(p).start();
    new Thread(p).start();

    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();
    new Thread(t).start();

    }

    }
  • 相关阅读:
    【逆序对相关/数学】【P1966】【NOIP2013D1T2】 火柴排队
    【贪心/DP/单调队列】【CF1029B】Creating the Contest
    【二分】【P1314】 【NOIP2011D2T2】聪明的质监员
    【树形DP】【P1351】 【NOIP2014D1T2】联合权值
    【枚举】 最大子矩阵(I)
    【单调队列】【P2627】 修剪草坪
    【矩阵】矩阵初级
    【计数】【UVA11401】 Triangle Counting
    【计数原理】【UVA11538】 Chess Queen
    【状压DP】【UVA11795】 Mega Man's Mission
  • 原文地址:https://www.cnblogs.com/toov5/p/8888657.html
Copyright © 2011-2022 走看看