00001 class Heap {
00002 static Connector c1, c2, c3, c4;
00003 }
00004
00005
00006 class PipeInt {
00007
00008
00009
00010
00011 static public void main (String[] args) {
00012 Heap.c1 = new Connector();
00013 Heap.c2 = new Connector();
00014 (new Stage1()).start();
00015
00016 Heap.c3 = new Connector();
00017 (new Stage2()).start();
00018
00019 Heap.c4 = new Connector();
00020 (new Stage3()).start();
00021
00022 (new Listener()).start();
00023
00024 for (int i = 1; i < 100; i++)
00025 Heap.c1.add(i);
00026
00027 c1StopCallLoc:
00028 Heap.c1.stop();
00029 }
00030 }
00031
00032 final class Connector {
00033 int queue = -1;
00034 public final synchronized int take() {
00035 int value;
00036 while (queue < 0)
00037 try {
00038 wait();
00039 } catch (InterruptedException ex) {}
00040 value = queue;
00041 queue = -1;
00042 return value;
00043 }
00044
00045 public final synchronized void add(int o) {
00046 queue = o;
00047 notifyAll();
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 public final synchronized void stop() {
00057 queue = 0;
00058 notifyAll();
00059 }
00060 }
00061
00062
00063
00064 final class Stage1 extends Thread {
00065
00066
00067
00068
00069
00070 public void run() {
00071 System.out.println("Stage1 startup");
00072 int tmp = -1;
00073 while (tmp != 0) {
00074 if ((tmp = Heap.c1.take()) != 0)
00075 Heap.c2.add(tmp + 1);
00076 }
00077 Heap.c2.stop();
00078
00079 stage1ShutdownLoc:
00080 System.out.println("Stage1 shutdown");
00081 }
00082 }
00083
00084 final class Stage2 extends Thread {
00085
00086
00087
00088
00089
00090 public void run() {
00091 System.out.println("Stage2 startup");
00092 int tmp = -1;
00093 while (tmp != 0) {
00094 if ((tmp = Heap.c2.take()) != 0)
00095 Heap.c3.add(tmp + 1);
00096 }
00097 stage2ShutdownLoc:
00098 Heap.c3.stop();
00099
00100 System.out.println("Stage2 shutdown");
00101 }
00102 }
00103
00104 final class Stage3 extends Thread {
00105
00106
00107
00108
00109
00110 public void run() {
00111 System.out.println("Stage3 startup");
00112 int tmp = -1;
00113 while (tmp != 0) {
00114 if ((tmp = Heap.c3.take()) != 0)
00115 Heap.c4.add(tmp + 1);
00116 }
00117 stage3ShutdownLoc:
00118 Heap.c4.stop();
00119
00120 System.out.println("Stage3 shutdown");
00121 }
00122 }
00123
00124 final class Listener extends Thread {
00125
00126
00127
00128
00129 public void run() {
00130 System.out.println("Listener startup");
00131 int tmp = -1;
00132 while (tmp != 0) {
00133 if ((tmp = Heap.c4.take()) != 0)
00134 System.out.println("output is " + tmp);
00135 }
00136
00137 listenerShutdownLoc:
00138 System.out.println("Listener shutdown");
00139 }
00140 }