日期:2014-05-20  浏览次数:20824 次

线程sleep()抛异常问题
import java.util.*;
public class TestInterrupt {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread thread = new MyThread();
thread.start();
try{
Thread.sleep(10000);
}catch(InterruptedException e){
thread.interrupt();
}
}

}
class MyThread extends Thread{
//boolean bl = true;
public void run(){
while(true){
System.out.println("========="+new Date()+"========");
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
}
10秒之后不终止,一直跑,哪位高手帮解释一下呗,万分感谢,本人初学者。

------解决方案--------------------
我只看到了你的interrupt是写在InterruptedException异常里面的。。。不interrupt就不会抛出异常,根本不可能进入catch里面的代码块,不知道视频里面是怎么样的。。。有点迷茫了,我也学了才不久呢
------解决方案--------------------
你给的视屏播放不成功;
没听说个让线程睡眠一段时间是异常,也不会抛出异常;