ThreadPoolExecutor的使用 (获取子线程执行结果后再执行主线程) 和CountDownLanch的简要使用

一、ThreadPoolExecutor 简要实例

  获取子线程执行结果后再执行主线程 ,这样可将复杂耗时业务拆分执行返回结果,将结果汇总整理。

多个线程时可以 利用Future阻塞,当其它线程执行完毕获得结果,再执行主线程
/**
     * ThreadPoolExecutor 线程池使用
     * 1、多个线程时可以 利用Future阻塞,当其它线程执行完毕获得结果,再执行主线程
      */
    @Test
    public void simpleMethod() {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(9, 100, 100, TimeUnit.SECONDS, new SynchronousQueue<>());
        try {
            List<String> list1 = new ArrayList<String>();

            Callable<Integer> call1 = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    for (int j = 0; j < 10; j++) {
                        list1.add("i:" + j);
                    }
                    return 10;
                }
            };
            Callable<Integer> call2 = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    for (int j = 0; j < 10; j++) {
                        list1.add("i:" + j);
                    }
                    return 10;
                }
            };
            Callable<Integer> call3 = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    for (int j = 0; j < 10; j++) {
                        list1.add("i:" + j);
                    }
                    return 10;
                }
            };

            Callable<Integer> call4 = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    for (int j = 0; j < 10; j++) {
                        list1.add("i:" + j);
                    }
                    return 10;
                }
            };
            Future<Integer> f1 = poolExecutor.submit(call1);
            Future<Integer> f2 = poolExecutor.submit(call2);
            Future<Integer> f3 = poolExecutor.submit(call3);
            Future<Integer> f4 = poolExecutor.submit(call3);
            ArrayList<String> ss = new ArrayList<>();
            f1.get();
            f2.get();
            f3.get();
            f4.get();
            // Thread.sleep(1000);
            System.out.println("list1.size() = " + list1.size());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

 二、CountDownLanch 简要实例

场景:当指定几个线程执行完后再执行 主线程,或使用2个CountDownLanch 对象进行多组子线程控制

/**
 * CountDownLatch  阻塞执行完预其的线程才会执行,后续方法业务处理
 */
@Test
public void lanchTest() {
    try {

        ThreadPoolExecutor thredPool = new ThreadPoolExecutor(20, 100, 1000, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
        CountDownLatch cutdownLanch = new CountDownLatch(3);
        thredPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是子线程-id:" + Thread.currentThread().getName());
                cutdownLanch.countDown();
            }
        });

        thredPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是子线程-id:" + Thread.currentThread().getName());
                cutdownLanch.countDown();
            }
        });

        thredPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是子线程-id:" + Thread.currentThread().getName());
                cutdownLanch.countDown();
            }
        });
        System.out.println("这是主线程在下一句阻塞,current-thread-id:" + Thread.currentThread().getName());
        cutdownLanch.await();
        System.out.println("子线程执行完毕");

    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

ps: 线程池各参数 先估算,再加上压力测试进行调整 。
原文地址:https://www.cnblogs.com/liyanbofly/p/15779084.html