多线程所有子线程执行完后再执行主线程,子线程并行执行

多线程所有子线程执行完后再执行主线程。 

方法一:

使用CountDownLatch 计数器

    public static void main(String[] args) throws Exception {
        int N = 10;// 线程个数
        CountDownLatch countDownLatch = new CountDownLatch(N);// 实例化一个倒计数器,N指定计数个数
        for (int i = 0; i < N; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println("子线程结束");
                        countDownLatch.countDown(); // 计数减一
                    }
                }
            }).start();
        }
        countDownLatch.await();// 阻塞,等待当计数减到0时,执行后面的代码
        System.out.println("结束");
    }

方法二:

使用线程池

    public static void main(String[] args) throws Exception {
        int N = 10;// 线程个数
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(N);
        for (int i = 0; i < N; i++) {
            fixedThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println("子线程结束");
                    }
                }
            });
        }
        //停止接收外部submit的任务
        fixedThreadPool.shutdown();
        // 等待子线程结束,再继续执行下面的代码
        fixedThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);//阻塞,等待所有子线程执行完成
        System.out.println("结束");
    }

模拟并行执行

使用CountDownLatch 计数器,模拟并行执行一个代码段

public class Test {
    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");

    public static void main(String[] args) throws Exception {
        Random random = new Random();
        int N = 10;// 线程个数
        CountDownLatch countDownLatch = new CountDownLatch(N);// 实例化一个倒计数器,N指定计数个数
        for (int i = 0; i < N; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int t = random.nextInt(N);//随机数
                        System.out.println("随机睡眠:" + t + "秒");
                        Thread.sleep(t * 1000);
                        countDownLatch.countDown(); // 计数减一
                        countDownLatch.await();// 阻塞,等待当计数减到0时,执行后面的代码

                        //并发执行打印方法
                        print();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    private static void print() {
        String time = LocalDateTime.now().format(dateTimeFormatter);
        System.out.println("并行代码执行时间:" + time);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值