JAVA執行緒的interrupt

2021-07-11 13:55:52 字數 3385 閱讀 7708

interrupt()

只是改變中斷狀態而已. 

interrupt()

不會中斷乙個正在執行的執行緒。這一方法實際上完成的是,給受阻塞的執行緒丟擲乙個中斷訊號,

這樣受阻執行緒就得以退出阻塞的狀態

。更確切 的說,如果執行緒被

object.wait, 

thread.join

和thread.sleep

三種方法之一阻塞,    那麼,它將接收到乙個

中斷異常

(interruptedexception

),從而

提早地終結被阻塞狀態

。如果執行緒沒有被阻塞,這時呼叫interrupt()將不起作用;否則,執行緒就將得到interruptedexception異常(該執行緒必須事先預備好處理此狀況),接著逃離阻塞狀態。

執行緒a在執行

sleep

,wait

,join

時,執行緒b

呼叫執行緒a的

interrupt

方法,的確這乙個時候a會有

interruptedexception 

異常丟擲來.

但這其實是在

sleep,wait,join

這些方法內部會不斷檢查中斷狀態的值,而自己丟擲的

interruptedexception

。如果執行緒a正在執行一些指定的操作時如賦值,for,while,if,呼叫方法等,都不會去檢查中斷狀態

,所以執行緒a不會丟擲 

interruptedexception

,而會一直執行著自己的操作.

當執行緒a終於執行到

wait(),sleep(),join()

具體使用見例項1,例項2。

注意1:當執行緒a執行到wait(),sleep(),join()時,丟擲interruptedexception後,

中斷狀態已經被系統復位了,

執行緒a呼叫thread.interrupted()返回的是false

public static boolean 

interrupted()

since: 

returns abooleanindicating whether the current thread (currentthread()) has a pending interrupt request (true) or not (false). it also has the side-effect of clearing the flag.

returns

public boolean 

isinterrupted()

since: 

returns abooleanindicating whether the receiver has a pending interrupt request (true) or not (false)

returns

1. sleep() & interrupt()

執行緒a正在使用sleep()暫停著: 

thread.sleep(100000);

如果要取消他的等待狀態,可以在正在執行的執行緒裡(比如這裡是b)呼叫

a.interrupt();

令執行緒a放棄睡眠操作,這裡a是執行緒a對應到的thread例項

當在sleep中時 執行緒被呼叫interrupt()時,就馬上會放棄暫停的狀態.並丟擲

interruptedexception

.丟出異常的,是a執行緒.

2. wait() & interrupt()

執行緒a呼叫了wait()進入了等待狀態,也可以用interrupt()取消.

不過這時候要小心鎖定的問題.執行緒在進入等待區,

會把鎖定解除

,當對等待中的執行緒呼叫

interrupt()時 ,會

先重新獲取鎖定,再丟擲異常.在獲取鎖定之前,是無法丟擲異常的.

3. join() & interrupt()

當執行緒以join()等待其他執行緒結束時,當它被呼叫interrupt(),它與sleep()時一樣, 會馬上跳到

catch

塊裡. 

注意例項1 :

static void

interruptdemo()

class

interruptdemorunnable implements runnable

interruptdemorunnable(thread t)

boolean blrun=true;

public void

run()

}else

catch(interruptedexception e)

trycatch(interruptedexception e)

}system.out.println(id+"run"+cnt);

cnt++;

if(cnt==5)

}system.out.println("thread:"+id+"exit");}}

注意1 :當執行緒a執行到wait(),sleep(),join()時,丟擲interruptedexception後,中斷狀態已經被系統復位了。

執行緒athread.interrupted()返回的是false

例項2 :

static void

interruptdemo()

class

interruptdemothread extends thread

interruptdemothread(thread t)

boolean blrun=true;

public void

run()

}else if(

isinterrupted()

)@1catch(interruptedexception e)

}thread.yield();

system.out.println(id+"run"+cnt);

cnt++;

if(cnt==5)

}system.out.println("thread:"+id+"exit");}}

注意1 :如果執行緒被呼叫了interrupt(),那麼如果此次該執行緒並不在wait(),sleep(),join()時,

下次執行

wait(),sleep(),join()時,一樣會丟擲interruptedexception,當然丟擲後該執行緒的中斷狀態也回被系統復位。

注意2:我們可以

手動的使用thread.interrupted()

來使當前執行緒的中斷狀態系統復位(即清除中斷狀態)

Thread中的中斷方法interrupted

thread.interrupt 方法,給執行緒新增乙個中斷標誌,不是意味著這個執行緒馬上結束,而是對執行緒起到一種通知的作用。兩種判斷中斷標誌的方法,一種是靜態方法interrupted,它可以獲取標誌,並且把中斷標誌清除,也就是返回fasle。還有一種方法是isinterrupted,它也可以獲...

java 執行緒 執行緒的通訊

執行緒通訊的三種方式 同步 while 輪詢方式 wait notify 方式 這裡講的同步是synchronized 關鍵字來實現執行緒間的通訊,我們用 來示範一下。class obj public synchronized void del public class demo4 start 執行...

Java執行緒 執行緒的排程 讓步

cpu資源,但是讓給誰不知道,僅僅是讓出,執行緒狀態回到可執行狀態。thread.yield 方法,yield 為靜態方法,功能是暫停當前正在執行的執行緒物件。執行緒 執行緒的排程 讓步 author leizhimin 2009 11 4 9 02 40 public class test cla...