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

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package testdemo1;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    class Mantou {
    
        private int id;
    
        public Mantou(int val) {
            this.id = val;
        }
    
        public String toString() {
            return "Mantou id: " + this.id;
        }
    }
    
    class Basket {
    
        private Mantou[] mStack = new Mantou[6];
        private int index = 0;
    
        public synchronized void push(Mantou m) {
            while (index == mStack.length) {
                try {
                     this.wait();
                    System.out.println("full........................!!!");
                   
                } catch (InterruptedException ex) {
                    Logger.getLogger(Basket.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.notify();
            
            mStack[index] = m;
    
            index++;
    
            System.out.println("add Mantou id= " + m + " and index= " + index);
           
    
        }
    
        public synchronized Mantou pop() {
    
            while (index == 0) {
    
                try {
                    System.out.println("empty........................!!!");
                    wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(Basket.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            this.notify();
            index--;
            Mantou m = mStack[index];
            System.out.println("get Mantou id= " + m + " and index= " + index);
    
            
            return m;
        }
    }
    
    class Producer implements Runnable {
    
        private Basket brasket = new Basket();
    
        public Producer(Basket val) {
            this.brasket = val;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                Mantou m = new Mantou(i);
                brasket.push(m);
    
                try {
                    //            Thread.sleep(Math);
                    Thread.sleep((int) (Math.random() * 500));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }
        }
    
    }
    
    class Consumer implements Runnable {
    
        private Basket brasket = new Basket();
    
        public Consumer(Basket val) {
            this.brasket = val;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 20; i++) {
                Mantou m = brasket.pop();
                try {
                    Thread.sleep((int) (Math.random() * 1000));
                } catch (InterruptedException ex) {
                    Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    public class TestDemo1 {
    
        public static void main(String[] args) {
            // TODO code application logic here
            Basket s = new Basket();
            Producer producer = new Producer(s);
            Consumer consumer = new Consumer(s);
            Thread tr1 = new Thread(producer);
            Thread tr2 = new Thread(consumer);
            tr1.start();
            tr2.start();
        }
    
    }
    


  • 相关阅读:
    在线教育项目-day11【JWT介绍】
    在线教育项目-day11【单点登录】
    在线教育项目-day11【添加redis缓存】
    在线教育项目-day11【前端显示】
    在线教育项目-day11【首页相关内容显示(后端接口)】
    兄弟俩畅游Tomcat城市的SpringMVC科技园区
    1小时让你掌握响应式编程,并入门Reactor
    【计算机基础】在0和1的世界里来来回回
    【面试】如果把线程当作一个人来对待,所有问题都瞬间明白了
    【面试】一篇文章帮你彻底搞清楚“I/O多路复用”和“异步I/O”的前世今生
  • 原文地址:https://www.cnblogs.com/MarchThree/p/3720415.html
Copyright © 2011-2022 走看看