设计思路:生产者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(); } } } }
原文:http://www.cnblogs.com/AceIsSunshineRain/p/5063005.html