Java多執行緒--讓主執行緒等待子執行緒執行完畢
阿新 • • 發佈:2018-11-30
參考連結:https://www.cnblogs.com/eoss/p/5902939.html
使用Java多執行緒程式設計時經常遇到主執行緒需要等待子執行緒執行完成以後才能繼續執行,那麼接下來介紹一種簡單的方式使主執行緒等待。
java.util.concurrent.CountDownLatch
使用countDownLatch.await()方法非常簡單的完成主執行緒的等待:
public class ThreadWait { public static void main(String[] args) throws InterruptedException { int threadNumber = 10; final CountDownLatch countDownLatch = new CountDownLatch(threadNumber); for (int i = 0; i < threadNumber; i++) { final int threadID = i; new Thread() { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(String.format("threadID:[%s] finished!!", threadID)); countDownLatch.countDown(); } }.start(); } countDownLatch.await(); System.out.println("main thread finished!!"); } }
CountDownLatch原始碼解析:
1.先看看await()方法:
public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } public final void acquireSharedInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg); } private void doAcquireSharedInterruptibly(int arg) throws InterruptedException { final Node node = addWaiter(Node.SHARED); boolean failed = true; try { for (;;) { final Node p = node.predecessor(); if (p == head) { int r = tryAcquireShared(arg); if (r >= 0) { setHeadAndPropagate(node, r); p.next = null; // help GC failed = false; return; } } if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) throw new InterruptedException(); } } finally { if (failed) cancelAcquire(node); } }
主要看parkAndCheckInterrupt()方法,就是是如何將主執行緒阻塞住的方法:
private final boolean parkAndCheckInterrupt() { LockSupport.park(this); //通過LockSupport.park()方法將執行緒交給系統阻塞; return Thread.interrupted(); }
2.看看countDown()方法,我們看看最終被countDown呼叫的unparkSuccessor()方法;
private void unparkSuccessor(Node node) { /* * If status is negative (i.e., possibly needing signal) try * to clear in anticipation of signalling. It is OK if this * fails or if status is changed by waiting thread. */ int ws = node.waitStatus; if (ws < 0) compareAndSetWaitStatus(node, ws, 0); /* * Thread to unpark is held in successor, which is normally * just the next node. But if cancelled or apparently null, * traverse backwards from tail to find the actual * non-cancelled successor. */ Node s = node.next; if (s == null || s.waitStatus > 0) { s = null; for (Node t = tail; t != null && t != node; t = t.prev) if (t.waitStatus <= 0) s = t; } if (s != null) LockSupport.unpark(s.thread); }
我們可以看到最終使用LockSupport.unpark()方法喚醒了主執行緒。
注:LockSupport類中的park與unpark方法都是使用的unsafe中的native本地方法;
最後我們來看一段最簡單的使用park與unpark方法阻塞喚醒執行緒程式碼:
public static void main(String[] args) { Thread t = new Thread(() -> { System.out.println("阻塞執行緒1"); LockSupport.park(); System.out.println("執行緒1執行完啦"); }); t.start(); try { Thread.sleep(2000); System.out.println("喚醒執行緒1"); LockSupport.unpark(t); Thread.sleep(2000); System.out.println("主執行緒結束"); } catch (InterruptedException e) { e.printStackTrace(); } }
執行結果:
阻塞執行緒1 喚醒執行緒1 執行緒1執行完啦 主執行緒結束