課程
/后端開(kāi)發(fā)
/Java
/深入淺出Java多線程
所以除了設(shè)置狀態(tài)標(biāo)志,還有什么退出線程的方法呢???
2018-07-17
源自:深入淺出Java多線程 3-3
正在回答
在catch中再次調(diào)用interrupt()就OK了
我是貓_夏 提問(wèn)者
慘的一
霸氣小肆毛 回復(fù) 慘的一
也可以在catch中寫(xiě)一個(gè) break;
在catch中加入interruput()后,有點(diǎn)不太明白,退出是能退出,但是Thread running少打印了一次啊
我愛(ài)孫佳怡
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循環(huán)包起來(lái)
舉報(bào)
帶你一起深入淺出多線程,掌握基礎(chǔ),展望進(jìn)階路線
1 回答最后的思考題,按照有的問(wèn)答提出的可以在catch中再次使用interrupt()方法,結(jié)果線程并沒(méi)有停下來(lái),求大神找找原因
3 回答最后的問(wèn)題能否這樣解答呢?
1 回答最后的總值為什么沒(méi)變
3 回答最后一句提問(wèn) 萌萌噠。。。差點(diǎn)射啦,
4 回答為什么在為什么在輸出了“程咬金的,,,,樂(lè)業(yè)”這一行后,兩方軍隊(duì)的進(jìn)攻批次最少是0最多是5呢???
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢(xún)優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-07-18
在catch中再次調(diào)用interrupt()就OK了
2019-07-04
也可以在catch中寫(xiě)一個(gè) break;
2019-05-22
在catch中加入interruput()后,有點(diǎn)不太明白,退出是能退出,但是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循環(huán)包起來(lái)