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

Therad问题.....求助
为什么下面那个程序不输出"Main thread interrupted"这句话;路过能解释一下吗,多谢了.
[code=Java][/code]class CurrentThreadDemo{
public static void main(String args[]){
Thread t = Thread.currentThread();
System.out.println("Current thread:" + t);
t.setName("My Thread");
System.out.println("After name change:" + t);
try{
for(int n=5;n>0;n--){
System.out.println(n);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
}
}

------解决方案--------------------
catch{}里面不能给答应语句
只能声明异常的类型
你应该没有弄清楚
try{}catch{throw}finally的关系
------解决方案--------------------
整个过程一切正常,没有受到意外中断啊。
异常抛出的的条件:在中断状态(被任意一线程中断)时调用sleep,他不会休眠。相反,他将清除这一状态并抛出InterruptedException