スレッド7.スレッドの停止
1155 ワード
スレッドの停止:スレッドの注意すべき実装を停止 スレッドを停止すると、一般的に変数に合わせて制御されます. 待機状態のスレッドを停止した場合、interruptメソッドと組み合わせて使用する必要があります.
public class Demo9 extends Thread {
boolean flag = true;
public Demo9(String name){
super(name);
}
@Override
public synchronized void run() {
int i = 0;
while(flag){
try {
this.wait(); // ...
} catch (InterruptedException e) {
System.out.println(" InterruptedException.."); //
}
System.out.println(Thread.currentThread().getName()+":"+ i);
i++;
}
}
public static void main(String[] args) {
//
Demo9 d = new Demo9(" ");
d.start();
// i 80 , 。
for(int i = 0 ; i<100 ; i++){
if(i==80){
// d.flag = false; //interrupt() ,
d.interrupt(); // wait、 sleep 。 。
}
System.out.println(Thread.currentThread().getName()+":"+i);
}
}