Thread.interrupted === interrupt() 無法真正中斷 thread,需要被呼叫的 thread 自己配合才行。 * 若 thread 處於阻塞狀態(EX:sleep, wait, join),則 thread 立即退出阻塞狀態,並拋出一個 InterruptedException * 若 thread 處於正常運行時,則該 thread 的 中斷flag 設為 true 而已。且此 thread 不受影響。 若 thread 有被中斷的需求時: 1. 在正常運行任務時,經常檢查 thread 自己的中斷 flag,若被設置了中斷 flag 就自行停止。 2. 客製化處理 InterruptedException(EX:回收資源,關閉 thread) ## Ref. [Java里一个线程调用了Thread.interrupt()到底意味着什么?](https://www.zhihu.com/question/41048032) ###### tags: `執行緒`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up