黑马程序员技术交流社区
标题:
结束冻结状态的线程示例
[打印本页]
作者:
肚小糖
时间:
2015-8-24 15:29
标题:
结束冻结状态的线程示例
class StopThread implements Runnable{
private boolean flag = true;
public void run(){
while(flag){
try{
wait();
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName()+"..."+e);
flag = false;
}
System.out.println(Thread.currentThread().getName()+"......");
}
}
public void setFlag(){
flag = false;
}
}
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 = 1;
for(;;){
if(++num == 50){
t1.interrupt();
t2.interrupt();
break;
}
System.out.println("main..."+num);
}
System.out.println("over");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2