java多线程并行查询记录

本文介绍了一种使用多线程提高数据处理效率的方法。通过将任务分解为多个子任务并行处理,有效地提升了处理速度。文章详细展示了如何创建固定大小的线程池,如何使用阻塞队列管理子任务,以及如何收集并整合处理结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 /***
     * 多线程处理业务
     * @param uids
     * @return
     * @throws Exception
     */
    public  List<OaLeaderInfoInTeamVO> dealLeaderListPensionByMutiThread(List<Long> uids, SearchUserListDTO searchUserListDTO) throws Exception{
        int nums = 50;
        int count=uids.size();
        int thrednum = (count % nums == 0) ? (count / nums) : (count / nums + 1);
        List<OaLeaderInfoInTeamVO> data = new ArrayList<>();
        ExecutorService service = Executors.newFixedThreadPool(thrednum);
        BlockingQueue<Future<List<OaLeaderInfoInTeamVO>>> queue = new LinkedBlockingQueue<Future<List<OaLeaderInfoInTeamVO>>>();
        for (int i = 0; i < thrednum; i++) {
            Future<List<OaLeaderInfoInTeamVO>> future = service.submit(read2List(i, nums,uids,searchUserListDTO));
            queue.add(future);
        }
        
        /**int queueSize = queue.size();
for (int i = 0; i < queueSize; i++) {
            List<OaLeaderInfoInTeamVO> list = queue.take().get();
            data.addAll(list);
        }*/
      while (queue.size() > 0) { 
            List<OaLeaderInfoInTeamVO> list = queue.take().get();
            data.addAll(list);
}
        service.shutdown();
        return  data;
    }
    private Callable<List<OaLeaderInfoInTeamVO>> read2List(final int i, final int nums,List<Long> uids, SearchUserListDTO searchUserListDTO ) {
        Callable<List<OaLeaderInfoInTeamVO>> callable = new Callable<List<OaLeaderInfoInTeamVO>>() {
            public List<OaLeaderInfoInTeamVO> call() throws Exception {
                int startIndex = i * nums;
                int maxIndex = startIndex + nums;
                if(maxIndex>uids.size()){
                    maxIndex=uids.size();
                }
                List<Long> subuids=Lists.newArrayList();
                subuids=uids.subList(startIndex,maxIndex);
                List<OaLeaderInfoInTeamVO> list=vipCenterService.getLeaderIdListInTeamWithExt(searchUserListDTO,subuids);
                return list;
            }
        };
        return callable;

    }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值