[問題] Thread interrupt的問題
大家好 小弟最近在學Threading
關於interrupt 我查了很多地方都沒寫得很清楚
所以我寫了下面這個程式
public class ThreadPlay extends Thread {
@Override
public void run() {
System.out.println(isInterrupted());
this.interrupt();
System.out.println(isInterrupted());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(isInterrupted());
}
public static void main(String[] args) {
ThreadPlay thr = new ThreadPlay();
thr.start();
}
}
執行後 三行println印出來分別是false, true, false
所以說 interrupt之後isInterrupted()會傳回true
但如果有InterruptedException 之後 又會被改回false?
請問這是Java原本的設定 還是跟我自己編譯器的解讀有關? 謝謝各位解答
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 128.12.247.107
※ 編輯: recorriendo 來自: 128.12.247.107 (07/22 06:24)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):