Несогласованные подпроцессы
Листинг 17.6. Несогласованные подпроцессы
class Store{
private inf inform;
synchronized public int getlnform(){ return inform; }
synchronized public void setlnform(int n){ inform = n; }
}
class Producer implements Runnable{
private Store st;
private Thread go;
Producer(Store st){
this.st = st;
go = new Thread(this);
go.start();
}
public void run(){
int n = 0;
Thread th = Thread.currentThread();
while(go == th){
st.setlnform(n);
System.out.print("Put: " + n + " ");
n++;
}
}
public void stop(){ go = null;
}
}
class Consumer implements Runnable{
private Store st;
private Thread go;
Consumer(Store st){
this.st = st;
go =-new Thread(this);
go.start () ;
}
public void run(){
Thread th = Thread.currentThread();
while(go == th) System.out.println("Got: " + st.getlnformf));
}
public void stop(){ go = null; }
}
class ProdCons{
public static void main(String[] args){
Store st = new Store();
Producer p = new Producer(st);
Consumer с = new Consumer(st);
try{
Thread.sleep(30);
}catch(InterruptedException ie){}
p.stop();
c.stop();
}
}