Package test;
Import java.util.LinkedList;
public class Test {public static void main (string[] args) {r r = new R ();
P p1 = new P (r,10);
P p2 = new P (r,20);
P p3 = new P (r,30);
P P4 = new P (r,40);
P P5 = new P (r,50);
c C1 = new C (r,10);
c C2 = new C (r,20);
c C3 = new C (r,30);
c C4 = new C (r,40);
c C5 = new C (r,50);
New Thread (p1). Start ();
New Thread (C1). Start ();
New Thread (P2). Start ();
New Thread (C2). Start ();
New Thread (P3). Start ();
New Thread (C3). Start ();
New Thread (P4). Start ();
New Thread (C4). Start ();
New Thread (P5). Start ();
New Thread (C5). Start ();
} class P implements runnable{private R R;
private int n;
Public P (R _r,int _n) {THIS.R = _r;
THIS.N = _n;
@Override public void Run () {R.push (n);
Class C implements runnable{private R R;
private int n;
Public C (R _r,int _n) {THIS.R = _r;
THIS.N = _n;
@Override public void Run () {R.pop (n); } class R {private Linkedlist<object> C = new linkedlist<object> ();
private static final int max_size = 100; public void push (int n) {synchronized (c) {if (C.size () +n <= max_size) {for (int i=0;i<n;i++) {C.add (NE
W Object ()); System.out.println ("Producer: Production of" +n+ "products, there are now a total of" +c.size () + "products, and wake up the consumer line program control can be consumed ....
");
C.notifyall (); }else{System.out.println ("Producer: number of products to be produced:" +n+ ", greater than the remaining space in the warehouse" + (Max_size-c.size ()) + "Current producer threads enter the waiting state ....
");
try {c.wait ();
catch (Interruptedexception e) {e.printstacktrace (); }}} public void pop (int n) {synchronized (c) {if (n <= c.size ()) {for (int i=0;i<n;i++) {C
. remove (); System.out.println ("Consumer: Consumption of" +n+ "products, there are still" +c.size () + "products, and wake-up producer line program control can be produced ....
");
C.notifyall (); }else{System.out.println ("Consumers: the number of products to be consumed:" +n+ ", greater than the number of warehouse surplus" +c.size () + "current consumer thread into the waiting state ....
");
try {c.wait ();
catch (Interruptedexception e) {e.printstacktrace ();
}
} }
}
}