亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我想請問關于interrupt()

我想請問關于interrupt()

hy_wang 2017-08-15 20:00:33
我想請問關于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()方法就會拋出異常 ?請教這是為什么?想不通
查看完整描述

3 回答

?
用戶1122125

TA貢獻2條經驗 獲得超0個贊

你在 run 里執行 sleep 是停的run 方法執行的那個線程

查看完整回答
反對 回復 2017-08-16
?
用戶1122125

TA貢獻2條經驗 獲得超0個贊

你直接調用 sleep? 是把主線程聽了3秒…

查看完整回答
反對 回復 2017-08-16
  • 3 回答
  • 0 關注
  • 1451 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號