zoukankan      html  css  js  c++  java
  • 生产者与消费者模式-阻塞 wait,notify

    设计思路:生产者push ,消费者 拿,篮子装,syncstack先进后出,while 判断 index=0 wait,      当 Producer生产了 并push到篮子里  notify(唤醒) 消费者。

    wait:发生了阻塞事件-篮子已经满了,并且不知道阻塞多长时间,要用wait

    public class ProducerConsumer {
        public static void main(String[] args) {
            SyncStack ss = new SyncStack();
            Producer p = new Producer(ss);
            Consumer c = new Consumer(ss);
            new Thread(p).start();
            new Thread(p).start();
            new Thread(p).start();
            new Thread(c).start();
        }
    }
    
    class WoTou {//馒头和馒头的属性
        int id; 
        WoTou(int id) {
            this.id = id;
        }
        public String toString() {
            return "WoTou : " + id;
        }
    }
    
    class SyncStack {//创建一个装馒头的“篮子”
        int index = 0;
        WoTou[] arrWT = new WoTou[6];
        
        public synchronized void push(WoTou wt) {//放馒头方法
            while(index == arrWT.length) {//一定要用while循环语句而不要用if,如果是if再wait阻塞被打断 报interruptedException  printStackTrace后会直接执行notifyall 而不会反过来头判断篮子里馒头满了没有
                try {
                    this.wait();//wait阻塞
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();        
            arrWT[index] = wt;
            index ++;
        }
        
        public synchronized WoTou pop() {
            while(index == 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notifyAll();
            index--;
            return arrWT[index];
        }
    }
    
    class Producer implements Runnable {
        SyncStack ss = null;
        Producer(SyncStack ss) {
            this.ss = ss;
        }
        
        public void run() {
            for(int i=0; i<20; i++) {
                WoTou wt = new WoTou(i);
                ss.push(wt);
    System.out.println("生产了:" + wt);
                try {
                    Thread.sleep((int)(Math.random() * 200));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }            
            }
        }
    }
    
    class Consumer implements Runnable {
        SyncStack ss = null;
        Consumer(SyncStack ss) {
            this.ss = ss;
        }
        
        public void run() {
            for(int i=0; i<20; i++) {
                WoTou wt = ss.pop();
    System.out.println("消费了: " + wt);
                try {
                    Thread.sleep((int)(Math.random() * 1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }            
            }
        }
    }
  • 相关阅读:
    C结构体之位域(位段)
    Linux 有效用户组(effective group)与初始用户组(initial group)(参考鸟哥私房菜)
    牛顿迭代法求平方根
    操作系统 第5章 哲学家问题
    面试题3:数组中重复的数字
    某讯实习生后台开发一面总结
    面试题目4:二维数组中的查找
    Java基础知识之基本概念
    Maven常用命令
    Maven仓库
  • 原文地址:https://www.cnblogs.com/AceIsSunshineRain/p/5063005.html
Copyright © 2011-2022 走看看