</p>public class ProducerConsumer {
H. j& I, x. u5 Q- E7 J/**( p" J$ O5 D i3 M9 ?
* @param args; P% b6 l6 j2 e/ }) a1 e ]+ b
*/
: ?) O% U8 A6 j. D9 B$ Upublic static void main(String[] args) {: P& L: k: Y+ N9 t; k; [, O" h
ProductBox pb = new ProductBox();
3 A2 H1 E6 G" ?% W. I' OProducer p = new Producer(pb);
$ r" z. `& k6 W' G: NConsumer c = new Consumer(pb);
# X& N# X+ Y/ uThread pThread = new Thread(p);! F$ J0 d* O3 S6 H
Thread cThread = new Thread(c);
+ p* Q! m% `2 B1 Z; xpThread.setPriority(Thread.MAX_PRIORITY);% h, ]: E* S4 M% ?
pThread.start();
$ o8 i1 l8 n# G6 B; ccThread.start();
9 }5 ]4 l. G* E: u$ |}7 Q& n! ^" P5 h7 y! \
}: `1 p9 L1 S' j) Q* v2 K* Y
/**3 t) F; ^' ]" N. K! f5 |9 y5 h
% Q" ?2 `" Z: b f/ f1 J% G* y; h' P; P
* x# ^& S- n# V q0 j
* 产物对象
' ^8 O. j" ~. }5 m4 b" b* @author johsnton678
( V# b% g9 i6 E6 T) r& Y* u7 k% b*/
! u% u! V- `: l) {1 M% L6 sclass Product {& H# r5 l* b- W: I! n$ Q
int id;) c) [3 u) K+ w0 @# r. a) l
public Product(int id) {( T) {$ V4 {# {' X8 k2 B; D
super();: u! W( @& \: D* p9 F( x7 t
this.id = id;8 H+ {% E ~8 O0 D. y; @
}/ n8 `9 m! w9 S! c- B4 L5 }" A
public String toString(){' `+ h) V- h' y" H
return “Product:” + id;
" x+ C6 X7 k% }! Y1 c}1 P& e0 c& ^7 l0 G2 y
}/ j( g- V# y. [# T% J; r. t
/** R) ?8 }, n! w; Q; C
* 产物盒对象
8 p1 P+ \- p/ o; ]* @author johnston678
, v: d5 o: K+ i9 q* z' y*/
# C8 H- q( s/ mclass ProductBox {
. g$ ?# V O- G% s$ uProduct[] productbox = new Product[6];8 |& D; t" u4 i x# X' O
int index = 0;
' A: O/ n5 ^4 l' Ipublic ProductBox() {
6 ]( ]/ O! U7 a( ^+ A2 Ssuper(); [4 F. q4 p1 h+ N7 D
}8 p' X7 `* w. c! ?+ V7 M' z" z
public synchronized void push(Product p) {- J7 [3 }) O% W8 v; I% ~' w) t
while (index == productbox.length) {7 x! b/ [7 r1 v" ?" Q
try {& G! ~. M( P5 Q; A5 ^: t
this.wait();) r1 ~. n# O0 B1 U
} catch (InterruptedException e) {
/ w3 F, j9 b1 s/ U" _6 v3 L1 {0 ?9 u% y3 N// TODO Auto-generated catch block
! f4 e$ Q* S3 \7 P# Ge.printStackTrace();
; t* ~' C& ]) D- c! Q* c}( T( _ L7 L: Z- M' j3 N
}8 }+ t z1 v( j5 e: O! Z5 E& }- d
this.notify();! l4 a* m S' B# L- T3 ~) ^
productbox[index] = p;2 r1 B: p" }+ `4 [' J
index ++;
! C, |' j+ p8 m; P p: U' G2 ^/ r1 j: ]}
1 v, @3 A- y3 V: t3 e0 _) t2 e0 P9 Xpublic synchronized Product pop() {
1 H% m8 U q0 X" A7 R. y: z9 ywhile (index == 0) {' `- y. v- W7 f1 h7 M
try {
( y9 p6 Z- @ T" X. b/ J* ?3 }7 fthis.wait();
* W5 d, m7 z+ h% F) \) |6 E& G} catch (InterruptedException e) {- Q/ m/ i7 M! {3 ~, |$ F
// TODO Auto-generated catch block
! a- F0 h$ ^9 y! B) ce.printStackTrace();8 S! ~4 F' a: a# n! v) V; A
}5 p& d( x, g2 B. @- A$ n8 S |: Y3 g
}% j. `4 Y) C1 t9 F" p2 a- p
this.notify();) z, o% ~8 G. t/ ~5 L+ K1 R
index --;
, Q- H8 h5 m7 [% W% X( Vreturn productbox[index];
; R+ ?, h, P) |}& b$ l7 y$ u; ]+ [6 U2 Y' J' y6 ~* j
}8 C3 e3 H j: |$ E) N9 X# |( x
/**
5 J+ ?/ h+ P$ S3 d* 出产者
7 V! R X. l* ]$ Z3 g+ O4 Y* @author johnston678
) L. A) C R# u2 p* ]8 a*/
: `* a2 H* a' Xclass Producer implements Runnable {1 Z4 d1 S% l+ U$ a4 v2 ~
ProductBox productbox = null;
& X2 W+ {0 n) {1 w8 V3 Lpublic Producer(ProductBox productbox) {
& h* A& e, ]6 O5 S! \$ @: {% @super();5 p4 ^$ {% r3 Y% E$ T) r/ e
this.productbox = productbox;
- _3 v% G7 ~5 x5 {" m' L& `8 _( T6 g9 |) Q* ^0 t
} |