Прекращение работы подпроцессов
Листинг 17.4. Прекращение работы подпроцессов
class TwoThreadsS implements Runnabie{
private String msg;
private Thread go;
TwoThreadsS(String s){
msg = s;
go = new Thread(this);
go.start();
}
public void run(){
Thread th = Thread.currentThread();
while(go == th){
try{
Thread.sleep(100);
}catch(InterruptedException ie){}
System.out.print(msg + " ");
}
System.out.println("End of thread.");
}
public void stop(){ go = null; }
public static void main(String[] args){
TwoThreadsS thl = new TwoThreadsS("HIP");
TwoThreadsS th2 = new TwoThreadsS("hop");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
thl.stop();
th2.stop();
System.out.printlnf);
}
}