日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

ThreadPoolExecutor(四)——Interrupt

發(fā)布時間:2024/3/13 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ThreadPoolExecutor(四)——Interrupt 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?1.補充知識

說ThreadPoolExecutor之前先要說先補充知識,關(guān)于Thread的interrupt相關(guān)操作。

還有一篇博客比較言簡意賅,interrupt、interrupted 、isInterrupted 區(qū)別

interrupt對于處于sleep,wait狀態(tài)的線程會進行interrupt并拋出InterruptedException,同時擦除中斷標(biāo)志位,也就是說,這時候如果再用isInterrupted或者靜態(tài)interrupted方法獲取中斷標(biāo)志位的時候,得到的是false。

interrupt對于正常運行的線程起不到中斷作用,只是把該線程的中斷標(biāo)志位設(shè)置為true了,線程會繼續(xù)正常運行。

Thread靜態(tài)interrupted方法判斷標(biāo)志位的時候也會清除標(biāo)志位。也就是說一個線程被中斷了,在線程中用interrupted判斷時返回true,再次調(diào)用該方法判斷會返回false。

Thread的成員函數(shù)isInterrupted判斷標(biāo)志位的時候不會清除標(biāo)志位,如果返回true,那么無論執(zhí)行多少次都會返回true(期間沒有其他清除標(biāo)志位的操作的話)。

2.處于sleep狀態(tài)下的打斷

public static void test1() throws Exception {final Thread thread = new Thread(new Runnable() {@Override public void run() {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();System.out.println("thread is interrupted!!!!!!!!!!2");System.out.println("isInterrupted:" + Thread.interrupted());}}});thread.start();new Thread(new Runnable() {@Override public void run() {// 會打斷上面那個線程的sleepthread.interrupt();}}).start();System.out.println("over");System.in.read();}

運行結(jié)果如下:

java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.qunar.flight.brandnew.java.thread.TInterrupt$1.run(TInterrupt.java:23)
at java.lang.Thread.run(Thread.java:722)
over
thread is interrupted!!!!!!!!!!2

說明處于sleep狀態(tài)的thread線程被中斷并拋出了異常。這里Thread.interrupted()方法返回是false,因為拋出異常之后就清除了中斷標(biāo)志位了。

3.處于wait狀態(tài)下的中斷

private static Object lock = new Object();public static void test2() throws Exception {final Thread thread = new Thread(new Runnable() {@Override public void run() {try {synchronized (lock) {lock.wait();}} catch (InterruptedException e) {e.printStackTrace();System.out.println("thread is interrupted!!!!!!!!!!1");}}});thread.start();new Thread(new Runnable() {@Override public void run() {System.out.println("interrupt");thread.interrupt();}}).start();System.out.println("over");System.in.read();}

結(jié)果:

over
interrupt
thread is interrupted!!!!!!!!!!
isInterrupted:false
java.lang.InterruptedException

這里和上面wait的情況基本一致。

4.線程正常運行時的interrupt

public static void test5() throws Exception {final Thread thread = new Thread(new Runnable() {@Override public void run() {for (int i = 0; i < 1000; i++) {for (int j = 0; j < 100000; j++) {System.currentTimeMillis();}}System.out.println("isInterrupted:" + Thread.interrupted());System.out.println("isInterrupted:" + Thread.currentThread().isInterrupted());}});thread.start();Thread thread2 = new Thread(new Runnable() {@Override public void run() {thread.interrupt();}});thread2.start();System.out.println("over");System.in.read();}

結(jié)果:

over
isInterrupted:true
isInterrupted:false這里中斷的時候thread線程依然正常運行,只是中斷標(biāo)志位被置位true了。Thread.interrupted()方法判斷的時候返回了true,同時擦除了中斷標(biāo)志位,所以再用Thread.currentThread().isInterrupted()判斷的時候,就返回false了。

5.處于park狀態(tài)下的interrupt

public static void test6() throws Exception {final Thread thread = new Thread(new Runnable() {@Override public void run() {LockSupport.park();System.out.println("isInterrupted:" + Thread.interrupted());System.out.println("isInterrupted:" + Thread.currentThread().isInterrupted());}});thread.start();Thread thread2 = new Thread(new Runnable() {@Override public void run() {thread.interrupt();}});thread2.start();System.out.println("over");System.in.read();}

結(jié)果:

over
isInterrupted:true
isInterrupted:false

和線程正常運行下的返回結(jié)果相同。

說明interrupt可以中斷處于park狀態(tài)的線程,但是不會拋出InterruptedException異常,只是設(shè)置中斷標(biāo)志位。

AbstractQueuedSynchronizer的doAcquireInterruptibly方法就是根據(jù)parkAndCheckInterrupt的返回值,在park狀態(tài)被喚醒的時候,判斷線程是被中斷的還是被unpark喚醒的,決定后續(xù)的操作是拋出異常還是繼續(xù)去獲得鎖
?

總結(jié)

以上是生活随笔為你收集整理的ThreadPoolExecutor(四)——Interrupt的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。