package algorithm; public class ProducerConsumer { public static void main(String[] args) { SyncStack ss = new SyncStack(); new Thread(new Producer(ss, "p1")).start(); new Thread(new Consumer(ss, "c1")).start(); new Thread(new Producer(ss, "p2")).start(); new Thread(new Consumer(ss, "c2")).start(); } } class WoTou { private int id; public WoTou(int id) { this.id = id; } public int getId() { return id; } public String toString() { return "WT" + getId(); } } class SyncStack { private int index = 0; private WoTou[] arrWT = new WoTou[6]; public synchronized void push(WoTou wt) { while (index == arrWT.length) { try { this.wait(); } catch (InterruptedException e) { } } this.notifyAll(); arrWT[index++] = wt; } public synchronized WoTou pop() { while (index == 0) { try { this.wait(); } catch (InterruptedException e) { } } this.notifyAll(); return arrWT[--index]; } } class Producer implements Runnable { private SyncStack ss = null; private String name; public Producer(SyncStack ss, String name) { this.ss = ss; this.name = name; } public String getName() { return name; } public void run() { for (int i = 0; i < 60; i++) { WoTou wt = new WoTou(i); ss.push(wt); System.out.println(getName() + "生产" + wt); try { Thread.sleep((long) (Math.random() * 100)); } catch (InterruptedException e) { } } } } class Consumer implements Runnable { private SyncStack ss = null; private String name; public Consumer(SyncStack ss, String name) { this.ss = ss; this.name = name; } public String getName() { return name; } public void run() { for (int i = 0; i < 60; i++) { WoTou wt = ss.pop(); System.out.println(getName() + "消费" + wt); try { Thread.sleep((long) (Math.random() * 400)); } catch (InterruptedException e) { } } } }
Output:
p1生产WT0
p2生产WT0
c1消费WT0
c2消费WT0
p2生产WT1
p1生产WT1
p2生产WT2
p2生产WT3
p2生产WT4
p2生产WT5
c2消费WT5
p2生产WT6
c1消费WT6
p1生产WT2
c2消费WT2
p1生产WT3
c2消费WT3
p2生产WT7
c1消费WT7
p1生产WT4
c2消费WT4
p1生产WT5
c2消费WT5
p2生产WT8
c1消费WT8
p2生产WT9
c1消费WT9
p1生产WT6
p2生产WT10
c2消费WT6
c1消费WT10
p2生产WT11
p2生产WT12
c2消费WT11
c1消费WT12
p2生产WT13
c2消费WT13
p2生产WT14
c1消费WT14
p1生产WT7
c1消费WT7
p2生产WT15
c2消费WT15
p2生产WT16
p2生产WT17
c2消费WT16
c1消费WT17
p2生产WT18
c2消费WT18
p2生产WT19
p2生产WT20
c1消费WT19
c2消费WT20
p2生产WT21
p1生产WT8
c2消费WT21
p2生产WT22
c2消费WT8
c1消费WT22
p1生产WT9
c2消费WT9
p1生产WT10
c1消费WT10
p1生产WT11
c2消费WT11
p2生产WT23
c2消费WT23
p2生产WT24
c1消费WT24
p1生产WT12
c1消费WT12
p2生产WT25
c2消费WT25
p2生产WT26
p2生产WT27
c1消费WT26
c1消费WT27
p1生产WT13
c1消费WT13
p1生产WT14
c2消费WT14
p2生产WT28
c2消费WT28
p2生产WT29
c1消费WT29
p2生产WT30
c2消费WT30
p2生产WT31
p2生产WT32
c1消费WT31
c2消费WT32
p2生产WT33
c1消费WT33
p2生产WT34
c2消费WT34
p2生产WT35
c1消费WT35
p1生产WT15
c1消费WT15
p1生产WT16
p2生产WT36
c2消费WT16
c2消费WT36
p1生产WT17
c2消费WT17
p2生产WT37
c1消费WT37
p1生产WT18
c1消费WT18
p1生产WT19
c2消费WT19
p1生产WT20
c1消费WT20
p1生产WT21
p1生产WT22
c2消费WT21
c1消费WT22
p1生产WT23
c1消费WT23
p1生产WT24
c2消费WT24
p1生产WT25
c2消费WT25
p2生产WT38
p1生产WT26
c2消费WT38
c2消费WT26
p1生产WT27
c2消费WT27
p1生产WT28
c1消费WT28
p1生产WT29
c2消费WT29
p1生产WT30
c2消费WT30
p1生产WT31
c1消费WT31
p1生产WT32
c2消费WT32
p1生产WT33
c1消费WT33
p1生产WT34
p2生产WT39
c2消费WT34
c2消费WT39
p1生产WT35
p1生产WT36
c1消费WT35
p2生产WT40
c2消费WT36
p2生产WT41
c2消费WT40
c2消费WT41
p2生产WT42
c1消费WT42
p2生产WT43
p1生产WT37
c1消费WT43
p1生产WT38
c2消费WT37
c1消费WT38
p2生产WT44
c2消费WT44
p2生产WT45
c1消费WT45
p2生产WT46
c1消费WT46
p2生产WT47
c2消费WT47
p2生产WT48
c1消费WT48
p1生产WT39
c1消费WT39
p2生产WT49
c2消费WT49
p2生产WT50
http://ixhong.iteye.com/blog/2228623