package thread;
/**
* 调用Thread.interrupt();进行强制清除冻结的线程
* 并可以控制标志位停止线程
* @author zjw
*
*/
public class Interrupted {
public static void main(String[] args) {
Inter_class in=new Inter_class();
Thread t=new Thread(in);
Thread t1=new Thread(in);
t.start();
t1.start();
for (int i = 0; i <100; i++) {
System.out.println(Thread.currentThread().getName()+"......"+i);
if(i==99){
t.interrupt();//调用Thread.interrupt();
t1.interrupt();//调用Thread.interrupt();
}
}
}
}
class Inter_class implements Runnable{
private int num;
private boolean boo=true;
public synchronized void run(){//加关键字
while(boo){
try{
wait();//该省略了this. 进入冻结状态
}catch(InterruptedException e){
boo=false;//控制标志位,停止线程
System.out.println("抛出Innterrupted异常");
}
System.out.println(Thread.currentThread().getName()+"--------"+num++);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public boolean isBoo() {
return boo;
}
public void setBoo(boolean boo) {
this.boo = boo;
}
}
黑马程序员——Thread.interrupt()清除线程冻结状态
最新推荐文章于 2022-07-22 10:47:44 发布