异步查询sql

/**
     * 异步查询产品列表
     *
     * @param productList
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    private List<Map<String, Object>> getPortTagsByList(List<Integer> productList) throws ExecutionException, InterruptedException {
        // 分割List
        List<List<Integer>> partitionList = Lists.partition(productList, 500);
        // 获取线程池
        ExecutorService executorService = Executors.newFixedThreadPool(15);
        // 返回结果
        List<Map<String, Object>> portTagResult = new ArrayList<>();
        List<Callable<List<Map<String, Object>>>> tasks = new ArrayList<>();
        for (List<Integer> list : partitionList) {
            Callable<List<Map<String, Object>>> batchGetPortTagsByIds = batchGetPortTagsByIds(list);
            tasks.add(batchGetPortTagsByIds);
        }
        //Future用于获取结果
        List<Future<List<Map<String, Object>>>> futures = executorService.invokeAll(tasks);
        //处理线程返回结果
        if (futures != null && futures.size() > 0) {
            for (Future<List<Map<String, Object>>> future : futures) {
                portTagResult.addAll(future.get());
            }
        }

        executorService.shutdown();//关闭线程池
        return portTagResult;
    }
/**
     * 获取Callable
     * @param productList
     * @return
     */
    private Callable<List<Map<String, Object>>> batchGetPortTagsByIds(List<Integer> productList) {
        return new Callable<List<Map<String, Object>>>() {
            public List<Map<String, Object>> call() throws Exception {
                return pDao.getPortTagsByIds(productList, "PORT");
            }
        };
    }

 

posted @ 2023-02-22 10:35  Lux1997  阅读(33)  评论(0)    收藏  举报