【Java并发】10个线程处理完任务后,再合并处理,CountDownLatch的用法

代码

线程一起执行:

import java.util.*;
import java.util.concurrent.*;

public class Main {

    public static List<String> getExecutorService() throws InterruptedException{
        System.out.println("开始执行多线程...");
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList<>(); //存放返回结果
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L,
                                                                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        for (int i = 0; i < 10; i++) {
            System.out.println("线程准备中...");
            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                        list.add(UUID.randomUUID().toString());
                        System.out.println("当前线程name : " + Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            };
            executorService.execute(runnable);
        }
        System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());
        countDownLatch.await();  // 主线程阻塞,等计数器==0,唤醒主线程往下执行。
        System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());
        System.out.println("submit总共cost 时间:" + (System.currentTimeMillis() - startTime)/1000 + "秒");
        executorService.shutdown();  // 等内部线程执行完,停止线程
        return list;
    }

    public static void main(String[] args) {
        List<String> res = new ArrayList<>();
        try {
            res = getExecutorService();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(res);
    }
}

一个一个线程执行

import java.util.*;
import java.util.concurrent.*;

public class Main {

    public static List<String> getExecutorService() throws InterruptedException{
        System.out.println("开始执行多线程...");
        long startTime = System.currentTimeMillis();
        List<String> list = new ArrayList<>();  // 存放返回结果
        CountDownLatch countDownLatch = new CountDownLatch(10);
        ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L,
                                                            TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
        for (int i = 0; i < 10; i++) {
            System.out.println("线程准备中...");
            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    list.add(UUID.randomUUID().toString());
                    System.out.println("当前线程name : " + Thread.currentThread().getName());
                    countDownLatch.countDown();
                }
            };
            executorService.execute(runnable);
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());
        countDownLatch.await();  // 主线程阻塞,等计数器==0,唤醒主线程往下执行。
        System.out.println("countDownLatch.getCount(): " + countDownLatch.getCount());
        System.out.println("submit总共cost 时间:" + (System.currentTimeMillis()-startTime)/1000 + "秒");
        executorService.shutdown();  // 等内部线程执行完,停止线程
        return list;
    }

    public static void main(String[] args) {
        List<String> res = new ArrayList<>();
        try {
            res = getExecutorService();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(res);
    }
}

结果

线程一起执行:

Connected to the target VM, address: '127.0.0.1:65178', transport: 'socket'
开始执行多线程...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
线程准备中...
countDownLatch.getCount(): 10
当前线程name : pool-1-thread-6
当前线程name : pool-1-thread-10
当前线程name : pool-1-thread-3
当前线程name : pool-1-thread-9
当前线程name : pool-1-thread-8
当前线程name : pool-1-thread-5
当前线程name : pool-1-thread-2
当前线程name : pool-1-thread-4
当前线程name : pool-1-thread-1
当前线程name : pool-1-thread-7
countDownLatch.getCount(): 0
submit总共cost 时间:3秒
[9f8d1b1d-21a3-4ba3-8a2c-9701126e900f, d37227bd-bf9b-4c18-a7ab-e8f2a621f8c6, 71c8b258-b973-46e5-b4dc-5aba9c3f4fb5, ad1d8d4a-bdb8-4a14-8d36-3b4c62a06f2f, 50be045c-0cec-4d52-bb23-1476d7e40bf9, e87f4359-4382-4b2f-b520-ae598a0ce53e, 7e854a60-2566-4797-a2f8-1217b0c19214, efb2ab95-d632-4dd8-bade-57635c65e3ea, a1a88c3a-7563-4110-bc97-9b7eb257994b, 27d7eb60-c83b-4a21-8f46-1b5170aa49c3]

线程一个一个执行:

开始执行多线程...
线程准备中...
当前线程name : pool-1-thread-1
线程准备中...
当前线程name : pool-1-thread-2
线程准备中...
当前线程name : pool-1-thread-3
线程准备中...
当前线程name : pool-1-thread-4
线程准备中...
当前线程name : pool-1-thread-5
线程准备中...
当前线程name : pool-1-thread-6
线程准备中...
当前线程name : pool-1-thread-7
线程准备中...
当前线程name : pool-1-thread-8
线程准备中...
当前线程name : pool-1-thread-9
线程准备中...
当前线程name : pool-1-thread-10
countDownLatch.getCount(): 0
countDownLatch.getCount(): 0
submit总共cost 时间:15秒
[2176b202-744d-4e8b-9ce7-dab3e02738ef, 40f1cd8b-22c8-4260-8d71-6e1c9c9f15db, 5dce872b-4aef-4ef8-8558-b5d5935e95b3, 2fd450ac-8db6-4f6b-8caf-62c6fa667253, 0846b4d2-5a42-4cbc-bdde-32ad5cbc03ec, 2451fade-1af0-4c0e-adf2-84469f599d64, 5285a9a3-3075-4f8a-99a5-5adee23662be, 4f867908-6e1b-4840-a050-945c59481f1c, 70ecacd3-fc5b-4896-8b5d-cb2ec7e434d8, d5b2a439-0a86-4a5f-94f5-07bf53b674f9]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锥栗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值