課程
/后端開發
/Java
/深入淺出Java多線程
所以除了設置狀態標志,還有什么退出線程的方法呢???
2018-07-17
源自:深入淺出Java多線程 3-3
正在回答
在catch中再次調用interrupt()就OK了
我是貓_夏 提問者
慘的一
霸氣小肆毛 回復 慘的一
也可以在catch中寫一個 break;
在catch中加入interruput()后,有點不太明白,退出是能退出,但是Thread running少打印了一次啊
我愛孫佳怡
package imooc.concurrent;
public class WrongWayStopThread extends Thread {
public static void main(String[] args) {
WrongWayStopThread thread = new WrongWayStopThread();
System.out.println("Staring thread...");
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Interrupting thread...");
thread.interrupt();
thread.keepRunning=false;
System.out.println("Stopping application...");
public volatile boolean keepRunning = true;
public void run() {
while (keepRunning) {
System.out.println("Thread is running...");
long time = System.currentTimeMillis();
while (System.currentTimeMillis() - time < 1000) {
還有就是用try{}catch{}把while循環包起來
舉報
帶你一起深入淺出多線程,掌握基礎,展望進階路線
1 回答最后的思考題,按照有的問答提出的可以在catch中再次使用interrupt()方法,結果線程并沒有停下來,求大神找找原因
3 回答最后的問題能否這樣解答呢?
1 回答最后的總值為什么沒變
3 回答最后一句提問 萌萌噠。。。差點射啦,
4 回答為什么在為什么在輸出了“程咬金的,,,,樂業”這一行后,兩方軍隊的進攻批次最少是0最多是5呢???
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-18
在catch中再次調用interrupt()就OK了
2019-07-04
也可以在catch中寫一個 break;
2019-05-22
在catch中加入interruput()后,有點不太明白,退出是能退出,但是Thread running少打印了一次啊
2018-08-23
package imooc.concurrent;
public class WrongWayStopThread extends Thread {
public static void main(String[] args) {
WrongWayStopThread thread = new WrongWayStopThread();
System.out.println("Staring thread...");
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Interrupting thread...");
thread.interrupt();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.keepRunning=false;
System.out.println("Stopping application...");
}
public volatile boolean keepRunning = true;
public void run() {
while (keepRunning) {
System.out.println("Thread is running...");
long time = System.currentTimeMillis();
while (System.currentTimeMillis() - time < 1000) {
}
}
}
}
2018-07-18
還有就是用try{}catch{}把while循環包起來