Java线程池 同步执行多个任务

    public R<String> tgFx(){
        long b = System.currentTimeMillis();

        // 多线程开始 分析指标
        ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 10, 200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(2));
        Callable tk1 =()->{
            long l = System.currentTimeMillis();
            String res = tgBasicInfoService.addTgBasicInfoAll();
            long l1 = System.currentTimeMillis() - l;
            System.out.println("<<<====线程【1】执行完成  耗时"+ l1+ " 毫秒" );
            return 1;
        };

        Callable tk2 =()->{
            long l = System.currentTimeMillis();
            tgBasicInfoService.queryPoUserRap();
            long l1 = System.currentTimeMillis() - l;
            System.out.println("<<<====线程【2】执行完成  耗时"+ l1+ " 毫秒" );
            return 1;
        };

        Callable tk3 =()->{
            long l = System.currentTimeMillis();
            tgBasicInfoService.queryAllTg96Load();
            long l1 = System.currentTimeMillis() - l;
            System.out.println("<<<====线程【3】执行完成  耗时"+ l1+ " 毫秒" );
            return 2;
        };


        Callable tk4 =()->{
            long l = System.currentTimeMillis();
            tgBasicInfoService.insertExpansionTg();
            long l1 = System.currentTimeMillis() - l;
            System.out.println("<<<====线程【4】执行完成  耗时"+ l1+ " 毫秒" );
            return 2;
        };


        List tasks = new ArrayList<>();
        tasks.add(tk1);
        tasks.add(tk2);
        tasks.add(tk3);
        tasks.add(tk4);

        long l2 = System.currentTimeMillis();
        long d = l2 - b;

        try {
            executor.invokeAll(tasks);
            return R.data("++++++++++++++++计算完成+++++++++++++++耗时==>"+CalculateUtils.div(String.valueOf(d),"1000",2)+"秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
            return R.data(e.getMessage());
        }finally {
            executor.shutdown();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值