package comm;
class StopThread implements Runnable {
private boolean flag = true;
public synchronized void run() {
while (flag) {
try {
wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()
+ "....exception");
flag = false; //interrupt后进入异常
}
System.out.println(Thread.currentThread().getName() + "....run");
}
}
}
public class StopThreadDemo {
public static void main(String[] args) {
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.start();
int num = 0;
while (true) {
if (num++ == 10) {
t1.interrupt(); //强制恢复
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName() + "------"
+ num);
}
}
}
interrupt()
最新推荐文章于 2022-11-01 15:38:09 发布