ThreadPoolExecutor(四)——Interrupt
?1.補充知識
說ThreadPoolExecutor之前先要說先補充知識,關于Thread的interrupt相關操作。
還有一篇博客比較言簡意賅,interrupt、interrupted 、isInterrupted 區別
interrupt對于處于sleep,wait狀態的線程會進行interrupt并拋出InterruptedException,同時擦除中斷標志位,也就是說,這時候如果再用isInterrupted或者靜態interrupted方法獲取中斷標志位的時候,得到的是false。
interrupt對于正常運行的線程起不到中斷作用,只是把該線程的中斷標志位設置為true了,線程會繼續正常運行。
Thread靜態interrupted方法判斷標志位的時候也會清除標志位。也就是說一個線程被中斷了,在線程中用interrupted判斷時返回true,再次調用該方法判斷會返回false。
Thread的成員函數isInterrupted判斷標志位的時候不會清除標志位,如果返回true,那么無論執行多少次都會返回true(期間沒有其他清除標志位的操作的話)。
2.處于sleep狀態下的打斷
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();}運行結果如下:
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狀態的thread線程被中斷并拋出了異常。這里Thread.interrupted()方法返回是false,因為拋出異常之后就清除了中斷標志位了。
3.處于wait狀態下的中斷
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();}結果:
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();}結果:
over
isInterrupted:true
isInterrupted:false這里中斷的時候thread線程依然正常運行,只是中斷標志位被置位true了。Thread.interrupted()方法判斷的時候返回了true,同時擦除了中斷標志位,所以再用Thread.currentThread().isInterrupted()判斷的時候,就返回false了。
5.處于park狀態下的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();}結果:
over
isInterrupted:true
isInterrupted:false
和線程正常運行下的返回結果相同。
說明interrupt可以中斷處于park狀態的線程,但是不會拋出InterruptedException異常,只是設置中斷標志位。
AbstractQueuedSynchronizer的doAcquireInterruptibly方法就是根據parkAndCheckInterrupt的返回值,在park狀態被喚醒的時候,判斷線程是被中斷的還是被unpark喚醒的,決定后續的操作是拋出異常還是繼續去獲得鎖。
?
總結
以上是生活随笔為你收集整理的ThreadPoolExecutor(四)——Interrupt的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java,图片在table中显示并缩放2
- 下一篇: 行业解决方案】高速公路供配电监控系统-安