Loading

hutool开启多线程

eg:https://blog.csdn.net/hanshanyunhai/article/details/121983861

@Test
public class AsyncTestController {

    /**
     * @author LiuWY
     * @description 异步
     * @date 2021/12/16
     * @param args
     * @return void
     */
    
    public static void main(String[] args) {
        try {
            for(int i=0;i<5;i++){
                ThreadUtil.execAsync(() -> {
                    int num = RandomUtil.randomInt(1000, 4000);
                    System.out.println(num);
                });
                System.out.println("第个线程:" + i);
            }
            
            System.out.println("完成");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 多线程实现线程等待的功能,所有线程的任务都指向完成后主线程才可以往下走
     * @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("第"+ i +"个线程" );
              //调用线程计数器-1
              countDownLatch.countDown();
            });
           
        }
        //唤醒主线程
        countDownLatch.await();
        System.out.println("完成");

    }
posted @ 2022-06-02 16:13  半瓶牛奶🥛  阅读(2506)  评论(0编辑  收藏  举报