Java多线程--让主线程等待子线程执行完毕(CountDownLatch类)

java.util.concurrent.CountDownLatch 

使用Java多线程编程时经常遇到主线程需要等待子线程执行完成以后才能继续执行,那么接下来介绍一种简单的方式使主线程等待。

  • CountDownLatch是一个同步辅助工具,用于使一个或多个线程等待(即阻塞)知道一组在其他线程中的任务结束。
  • CountDownLatch必须用给定的count(一个int类型的大于等于0的值)进行初始化。调用await方法将使线程阻塞,直到当前计数(count值)由于countdown方法的调用而达到零,此后所有等待的线程被释放并且任何后续调用await方法也会立即返回。CountDownLatch被设计为只触发一次,即Count值在运行过程中无法重置。如果需要重置计数的版本,可以考虑使用CyclicBarrier.
  • CountDownLatch是一种通用的同步工具。 CountDownLatch可以被认为是一个简单的on/off锁存器或门:所有线程调用await方法等待开关打开,直到countDown方法被调用打开开关为止。 创建一个CountDownLatch,指定count的值为N,那么这个CountDownLatch对象可以让一个线程等待其他N个线程结束(调用countDown方法即认为结束),或者调用了这个CountDownLatch的countDown方法N次。

测试代码如下:

  1. public class ThreadWait {
  2. public static void main(String[] args) throws InterruptedException {
  3. ExecutorService exector = Executors.newFixedThreadPool(5);
  4. int threadNumber = 13;
  5. final CountDownLatch countDownLatch = new CountDownLatch(threadNumber);
  6. for (int i = 0; i < threadNumber; i++) {
  7. final int threadID = i;
  8. exector.execute(
  9. () -> {
  10. try {
  11. Thread.sleep(2000);
  12. System.out.println(String.format("threadID:[%s] finished!!", threadID));
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. } finally {
  16. countDownLatch.countDown(); //这个不管是否异常都需要数量减,否则会被堵塞无法结束
  17. }
  18. }
  19. );
  20. }
  21. countDownLatch.await();//保证之前的所有的线程都执行完成,才会走下面的
  22. System.out.println(countDownLatch.getCount());
  23. System.out.println("main thread finished!!");
  24. }
  25. }

结果为:

  1. threadID:[0]finished!!
  2. threadID:[1]finished!!
  3. threadID:[4]finished!!
  4. threadID:[3]finished!!
  5. threadID:[2]finished!!
  6. threadID:[9]finished!!
  7. threadID:[8]finished!!
  8. threadID:[5]finished!!
  9. threadID:[6]finished!!
  10. threadID:[7]finished!!
  11. threadID:[10]finished!!
  12. threadID:[11]finished!!
  13. threadID:[12]finished!!
  14. 0
  15. main thread finished!!

CountDownLatch源码解析:

1.构造器

构造器方法,必须指定count值,且count值不能小于0,注释的意思:创建CountDownLatch对象时需指定count值,count值即当前线程从调用await方法时处于阻塞状态转换到就绪状态时countDown方法必须调用的次数!

2.await()方法:

  1. public void await() throws InterruptedException {
  2. sync.acquireSharedInterruptibly(1);
  3. }
  4. public final void acquireSharedInterruptibly(int arg)
  5. throws InterruptedException {
  6. if (Thread.interrupted())
  7. throw new InterruptedException();
  8. if (tryAcquireShared(arg) < 0)
  9. doAcquireSharedInterruptibly(arg);
  10. }
  11. private void doAcquireSharedInterruptibly(int arg)
  12. throws InterruptedException {
  13. final Node node = addWaiter(Node.SHARED);
  14. boolean failed = true;
  15. try {
  16. for (;;) {
  17. final Node p = node.predecessor();
  18. if (p == head) {
  19. int r = tryAcquireShared(arg);
  20. if (r >= 0) {
  21. setHeadAndPropagate(node, r);
  22. p.next = null; // help GC
  23. failed = false;
  24. return;
  25. }
  26. }
  27. if (shouldParkAfterFailedAcquire(p, node) &&
  28. parkAndCheckInterrupt())
  29. throw new InterruptedException();
  30. }
  31. } finally {
  32. if (failed)
  33. cancelAcquire(node);
  34. }
  35. }

主要看parkAndCheckInterrupt()方法,就是是如何将主线程阻塞住的方法:

  1. private final boolean parkAndCheckInterrupt() {
  2. LockSupport.park(this); //通过LockSupport.park()方法将线程交给系统阻塞;
  3. return Thread.interrupted();
  4. }

await方法使当前线程等待直到count值为0,或者当前线程被打断!如果当前的count值为0,那么await方法直接返回,当前线程不会阻塞!如果当前的count值大于0,那么当前线程阻塞(线程调度机制无法给当前线程分配CPU时间片),直到以下两种情况任意一种发生为止:

  1. count值通过countDown方法的调用达到0
  2. 其他线程打断了当前线程

3.countDown()方法,我们看看最终被countDown调用的unparkSuccessor()方法;

  1. private void unparkSuccessor(Node node) {
  2. /*
  3. * If status is negative (i.e., possibly needing signal) try
  4. * to clear in anticipation of signalling. It is OK if this
  5. * fails or if status is changed by waiting thread.
  6. */
  7. int ws = node.waitStatus;
  8. if (ws < 0)
  9. compareAndSetWaitStatus(node, ws, 0);
  10. /*
  11. * Thread to unpark is held in successor, which is normally
  12. * just the next node. But if cancelled or apparently null,
  13. * traverse backwards from tail to find the actual
  14. * non-cancelled successor.
  15. */
  16. Node s = node.next;
  17. if (s == null || s.waitStatus > 0) {
  18. s = null;
  19. for (Node t = tail; t != null && t != node; t = t.prev)
  20. if (t.waitStatus <= 0)
  21. s = t;
  22. }
  23. if (s != null)
  24. LockSupport.unpark(s.thread);
  25. }

我们可以看到最终使用LockSupport.unpark()方法唤醒了主线程。

注:LockSupport类中的park与unpark方法都是使用的unsafe中的native本地方法;

当前的count值减一,如果count值为0则释放所有等待的线程!如果当前count值大于0,则减一,如果count值为0,则所有等待的线程对于线程调度机制来说都是活跃的了! 

最后我们来看一段最简单的使用park与unpark方法阻塞唤醒线程代码:

  1. public static void main(String[] args) {
  2. Thread t = new Thread(() -> {
  3. System.out.println("阻塞线程1");
  4. LockSupport.park();
  5. System.out.println("线程1执行完啦");
  6. });
  7. t.start();
  8. try {
  9. Thread.sleep(2000);
  10. System.out.println("唤醒线程1");
  11. LockSupport.unpark(t);
  12. Thread.sleep(2000);
  13. System.out.println("主线程结束");
  14. } catch (InterruptedException e) {
  15. e.printStackTrace();
  16. }
  17. }

运行结果:

  1. 阻塞线程1
  2. 唤醒线程1
  3. 线程1执行完啦
  4. 主线程结束

转载自:https://www.cnblogs.com/eoss/p/5902939.html