hutool实现多线程并发执行并等待的功能

package com.system;

import cn.hutool.core.thread.ThreadUtil;
import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

public class ThreadUtilTest {
    /**
     * 多线程实现线程等待的功能,所有线程的任务都指向完成后主线程才可以往下走
     * @throws InterruptedException
     */
    public static void test1() throws InterruptedException {
        CountDownLatch countDownLatch=ThreadUtil.newCountDownLatch(5);
        for(int i=0;i<5;i++){
            ThreadUtil.execute(() -> {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("第个线程");
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("完成");

    }
    public static void main(String[] args) throws InterruptedException {
        test1();
    }
}

你可能感兴趣的:(多线程,java)