线程停止
1.建议线程自己正常停止------> 利用次数,不建议死循环。
2.设置标志位----->
3.不用stop destory 方法。
public class TestStop implements Runnable{
//1. 设置标志位
prviate boolean flag= true;
public void run(){
int i=0;
while (flag){
System.out.println("run...Thread0"+i++);
}
}
//2.设置一个公开方法停止线程,转换标志位
public void stop(){
this.flag=false;
}
public static void main(String[] args){
TestStop teststop =new TestStop();
new Thread(teststop).start();
for(int i=0;i<1000;i++){
System.out.println("main"+i);
if(i=900){
teststop.stop();
System.out.println("线程停止");
}
}
}
}

浙公网安备 33010602011771号