使用runnable 处理大数量查询

原文链接:https://blog.csdn.net/dearKundy/article/details/87649405

作者:你在我家门口

https://blog.csdn.net/dearKundy/article/details/87649405

在原有的基础上我使用了Runnable接口实现了下这个功能


    private void multiThreadListProduct2(){

        List<ProductionInfo> list=new ArrayList<>();
        long startTime=System.currentTimeMillis();
        int totalNum=travelMapper.countProductInfo();
        int loopNum=new Double(Math.ceil((double)totalNum/THREAD_MAX_ROW2)).intValue();
        System.out.println("开始查询,查询总数"+totalNum+",查询次数"+loopNum+"");
        CountDownLatch countDownLatch=new CountDownLatch(loopNum);
        executeTask1(totalNum,loopNum,list,countDownLatch);
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()-startTime);

        System.out.println(list.size());

    }

    private void executeTask1(int totalNum, int loopNum, List<ProductionInfo> list,CountDownLatch countDownLatch) {
        for(int i=0;i<loopNum;i++){
            Map<String,Object> map=new HashMap<>();
            map.put("offset",THREAD_MAX_ROW2*i);
            map.put("limit",THREAD_MAX_ROW2);
            if(i==loopNum-1){
                map.put("limit",totalNum-THREAD_MAX_ROW2*i);
            }
            ThreadA threadA=new ThreadA(list,map,countDownLatch);
            new Thread(threadA).start();
        }
    }

    private class ThreadA implements Runnable{
        private List<ProductionInfo> list;
        private Map<String,Object> map;
        private CountDownLatch countDownLatch;

        public ThreadA(List<ProductionInfo> list,Map<String,Object> map,CountDownLatch countDownLatch) {
            this.list=list;
            this.map=map;
            this.countDownLatch=countDownLatch;
        }

        @Override
        public void run() {
            List<ProductionInfo> list1=travelMapper.listProduct(map);
            list.addAll(list1);
            countDownLatch.countDown();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值