java 执行多个子线程后执行主线程

           // 不止一页,则需要分页调用
            String totalStr = accountInfo.getTotal();
            int totalInt = Integer.parseInt(totalStr);
            final int pageSize = 2000; //内部类调用需定义为私有
            int pageNum = totalInt%pageSize == 0 ? totalInt/pageSize : totalInt/pageSize+1;
            // 保存微服务返回来的数据
            saveRpcCustomers(accountInfo.getRows(), batchNo);
            
            //1、 创建CountDownLatch 对象, 设定需要计数的子线程数目
            final CountDownLatch latch = new CountDownLatch(pageNum-1);
             
            for(int i=2;i<=pageNum;i++)
            {
                final int f = i;
                new Thread(){

                    @Override
                    public void run() {
                        TcmAccountInfo accountInfoTemp = TcmAccountInfoService.batchQueryClientDetails(String.valueOf(f),String.valueOf(pageSize));
                        saveRpcCustomers(accountInfoTemp.getRows(), batchNo);
                        //2、子线程执行完毕,计数减1
                        latch.countDown();
                    }
                    
                    
                }.start();
                
            }
            //3、 当前线程挂起等待 ,所有线程执行完,才往下执行主线程
            latch.await();
             
            // 将中间表中的数据刷新到正式表中
            integrationDao.temp2Customer(batchNo);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值