我想請(qǐng)問(wèn)關(guān)于interrupt()這個(gè)方法調(diào)用什么時(shí)候才可以?huà)伋霎惓??老師講解是在線程被阻塞的時(shí)候調(diào)用會(huì)拋出異常。package?one;
public?class?newnew?extends?Thread?{
?public?void?run(){
?????????
????????System.out.println("線程正在運(yùn)行");
????????long?time?=?System.currentTimeMillis();
????????while((System.currentTimeMillis()-time)<1000){}
????}
?????
????public?static?void?main(String[]?args)?{
?????????
???? newnew?wwst?=?new?newnew();
????????System.out.println("啟動(dòng)線程?");
????????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("線程結(jié)束了");
?????????
}
}
可是請(qǐng)問(wèn)關(guān)于這段代碼?我在調(diào)用wwst.interrupt();的時(shí)候上邊明明有
?sleep(3000);??運(yùn)行的時(shí)候卻沒(méi)有拋出異常
?而如果我?在run()方法中?假如sleep()方法就會(huì)拋出異常
?請(qǐng)教這是為什么?想不通
添加回答
舉報(bào)
0/150
提交
取消