[英]where is interrupt status flag of a thread in java , and why is interrupted() method static
[英]method interrupted in java,when use it inside while loop will interrupt the thread or not?
如果我使用這種方法,它將中斷線程嗎?
while ( !interrupted() && more work to do){ /* main thread execution loop*/ }
第二個問題:這句話是什么意思?
interrupt()方法,並不自動意味着該線程應該終止。 它只是吸引了線程的注意力(就像在戳一個熟睡的室友來喚醒他一樣)
調用interrupted()
不會“中斷”線程中代碼的執行。 但是,如果循環中沒有其他代碼調用interrupted()
(清除當前線程的中斷狀態),則運行該線程的線程被另一個調用interrupt()的線程中斷時,您的情況將導致循環退出。
您所引述的解釋,一個線程的代碼必須作用於一個中斷,通過檢查線程的中斷狀態的文本,或致電能夠投擲InterruptedException的(或方法ClosedByInterruptException )。 如果線程的代碼不執行任何操作,則在該線程上調用interrupt()
將不會對代碼執行產生任何影響。 它只會設置線程的中斷狀態,這只是線程的布爾屬性。 代碼必須正確響應中斷才能使中斷生效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.