zoukankan      html  css  js  c++  java
  • 多线程,生产者消费者模型(生产馒头,消费馒头)

    先建立一个容器

    /**
     * 容器
     * 共享资源
     * @author Administrator
     *
     */
    public class SynStack {
        
        int index = 0;
        //容器
        SteamBread[] stb = new SteamBread[6];
        
        /**
         * 往容器中放产品
         */
        public synchronized void push(SteamBread st){
            while(index == stb.length){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            notify();//唤醒正在等待的线程
            stb[index] = st;
            this.index++;
        }
        /**
         * 从容器中取产品
         */
        public synchronized SteamBread pop(){
            while(index == 0){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            notify();
            this.index--;
            return stb[index];
        }
    
    }

    产馒头

    package com.newer.cn;
    
    public class Producer implements Runnable{
        SynStack ss = null;
        
        public Producer(SynStack ss) {
            // TODO Auto-generated constructor stub
            this.ss = ss;
        }
        @Override
        public void run() {
            //开始生产馒头
            for(int i = 1;i <= 20;i++){
                SteamBread stb = new SteamBread(i);
                System.out.print("生产了::::::");
                ss.push(stb);
                System.out.println("生产了"+stb);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    
    }

    消费馒头

    package com.newer.cn;
    
    
    public class Consume implements Runnable{
        SynStack ss = null;
        
         public Consume(SynStack ss) {
             this.ss = ss;
        }
        @Override
        public void run() {
            //开始消费馒头
            for(int i = 1;i <= 20;i++){
                System.out.print("消费了::::::");
                SteamBread stb = ss.pop();
                System.out.println("消费了"+stb);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
    
    }

    测试

    package com.newer.cn;
    
    public class Test2 {
    
        public static void main(String[] args) {
            SynStack ss = new SynStack();
            //生产者线程
            Producer p = new Producer(ss);
            Thread tp = new Thread(p);
            
            //消费者线程
            Consume c = new Consume(ss);
            Thread tc = new Thread(c);
            
            tp.start();
            tc.start();
        }
    
    }
  • 相关阅读:
    20145316《Java程序设计》第七周学习总结
    第三周博客问题总结
    20145316《Java程序设计》第六周学习总结
    20145316《Java程序设计》实验一:Java开发环境的熟悉(Windows + IDEA)
    八、九章知识点整理
    20145316许心远《Java程序设计》第5周学习总结
    第六章:继承与多态(知识梳理/查漏补缺)
    20145316许心远《Java程序设计》第4周学习总结
    实现最大索引堆
    Java排序算法全
  • 原文地址:https://www.cnblogs.com/lujing-newer/p/6607791.html
Copyright © 2011-2022 走看看