1. 倒计时门闩
    2. 设定一个值,完成一个进行一次countDown,结束之后就开了(await的地方就能继续向下了)
    3. 门闩在那拴住门,不让他向下执行\
    4. 滚动发车===>坐满就走
    5. 与join实现的功能差不多
    6. 与join效率相比应该差不多===>但是一般这种阻塞的方法不谈效率===>运行的线程运行多长时间才应该算效率
    7. 门闩控制得比join灵活不少!!!(可以更加个性化)
    1. package com.mashibing.juc.c_020;
    2. import java.util.concurrent.CountDownLatch;
    3. public class T06_TestCountDownLatch {
    4. public static void main(String[] args) {
    5. usingJoin();
    6. usingCountDownLatch();
    7. }
    8. private static void usingCountDownLatch() {
    9. Thread[] threads = new Thread[100];
    10. CountDownLatch latch = new CountDownLatch(threads.length);
    11. for(int i=0; i<threads.length; i++) {
    12. threads[i] = new Thread(()->{
    13. int result = 0;
    14. for(int j=0; j<10000; j++) result += j;
    15. latch.countDown();
    16. });
    17. }
    18. for (int i = 0; i < threads.length; i++) {
    19. threads[i].start();
    20. }
    21. try {
    22. latch.await();
    23. } catch (InterruptedException e) {
    24. e.printStackTrace();
    25. }
    26. System.out.println("end latch");
    27. }
    28. private static void usingJoin() {
    29. Thread[] threads = new Thread[100];
    30. for(int i=0; i<threads.length; i++) {
    31. threads[i] = new Thread(()->{
    32. int result = 0;
    33. for(int j=0; j<10000; j++) result += j;
    34. });
    35. }
    36. for (int i = 0; i < threads.length; i++) {
    37. threads[i].start();
    38. }
    39. for (int i = 0; i < threads.length; i++) {
    40. try {
    41. threads[i].join();
    42. } catch (InterruptedException e) {
    43. e.printStackTrace();
    44. }
    45. }
    46. System.out.println("end join");
    47. }
    48. }