1700449751
1700449752
(1)stop方法是过时的
1700449753
1700449754
从Java编码规则来说,已经过时的方法不建议采用。
1700449755
1700449756
(2)stop方法会导致代码逻辑不完整
1700449757
1700449758
stop方法是一种“恶意”的中断,一旦执行stop方法,即终止当前正在运行的线程,不管线程逻辑是否完整,这是非常危险的。看如下的代码:
1700449759
1700449760
public static void main(String[]args)throws Exception{
1700449761
1700449762
//子线程
1700449763
1700449764
Thread thread=new Thread(){
1700449765
1700449766
@Override
1700449767
1700449768
public void run(){
1700449769
1700449770
try{
1700449771
1700449772
//子线程休眠1秒
1700449773
1700449774
Thread.sleep(1000);
1700449775
1700449776
}catch(InterruptedException e){
1700449777
1700449778
//异常处理
1700449779
1700449780
}
1700449781
1700449782
System.out.println(“此处代码不会执行”);
1700449783
1700449784
}
1700449785
1700449786
};
1700449787
1700449788
//启动线程
1700449789
1700449790
thread.start();
1700449791
1700449792
//主线程休眠0.1秒
1700449793
1700449794
Thread.sleep(100);
1700449795
1700449796
//子线程停止
1700449797
1700449798
thread.stop();
1700449799
1700449800
}
[
上一页 ]
[ :1.700449751e+09 ]
[
下一页 ]