• 多线程,生产者消费者模型(生产馒头,消费馒头)


    先建立一个容器

    /**
     * 容器
     * 共享资源
     * @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();
        }
    
    }
  • 相关阅读:
    转:马云邮件全文
    XIFF资料1
    代码还是请一个字母一个字母敲(如果您只想混口饭吃就不要读了本文只面向想成为hacker的程序员)
    一个本来很有希望的项目噶然而止,脑子一下子空了
    转:进京感受一个技术人职业发展心得
    java中定义接口
    两个大数相乘(纯C实现)
    [ios学习入门1]hello,word!
    两台电脑通信的连接过程
    谁说引用不可改变
  • 原文地址:https://www.cnblogs.com/lujing-newer/p/6607791.html
Copyright © 2020-2023  润新知