我想請問關于interrupt()這個方法調用什么時候才可以拋出異常?老師講解是在線程被阻塞的時候調用會拋出異常。package?one;
public?class?newnew?extends?Thread?{
?public?void?run(){
?????????
????????System.out.println("線程正在運行");
????????long?time?=?System.currentTimeMillis();
????????while((System.currentTimeMillis()-time)<1000){}
????}
?????
????public?static?void?main(String[]?args)?{
?????????
???? newnew?wwst?=?new?newnew();
????????System.out.println("啟動線程?");
????????wwst.start();
?????????
????????try?{
????????????sleep(3000);
????????}?catch?(InterruptedException?e)?{
????????????e.printStackTrace();
????????}
?????????
????????System.out.println("中斷線程");
????????wwst.interrupt();
?????????
????????try?{
????????????sleep(3000);
????????}?catch?(InterruptedException?e)?{
????????????e.printStackTrace();
????????}
????????System.out.println("線程結束了");
?????????
}
}
可是請問關于這段代碼?我在調用wwst.interrupt();的時候上邊明明有
?sleep(3000);??運行的時候卻沒有拋出異常
?而如果我?在run()方法中?假如sleep()方法就會拋出異常
?請教這是為什么?想不通
添加回答
舉報
0/150
提交
取消