1. 程式人生 > >java多執行緒並行查詢記錄

java多執行緒並行查詢記錄

 /***
     * 多執行緒處理業務
     * @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);
        }
        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;

    }