Use concurrent methods to query data

The first step: define a multi-threaded class, inherit the Thred class

public class GetCommodityThread extends Thread {
    
    

    private final static Logger LOGGER = LoggerFactory.getLogger(GetCommodityThread.class);

    private BasicDataFeignClientService basicDataFeignClientService;

    private CountDownLatch countDownLatch;

    private GoodsIdsReq goodsIdsReq;

    private List<Map<String, GoodsVO>> mapList;

    public GetCommodityThread(BasicDataFeignClientService basicDataFeignClientService, CountDownLatch countDownLatch,
                              GoodsIdsReq goodsIdsReq, List<Map<String, GoodsVO>> mapList) {
    
    
        this.basicDataFeignClientService = basicDataFeignClientService;
        this.countDownLatch = countDownLatch;
        this.goodsIdsReq = goodsIdsReq;
        this.mapList = mapList;
    }

    @Override
    public void run() {
    
    
        this.doGet();
        countDownLatch.countDown();
        LOGGER.info("还剩余线程数:" + countDownLatch.getCount());
    }

    private void doGet() {
    
    
        ResultData<Map<String, GoodsVO>> commodityMapByIds = basicDataFeignClientService.getCommodityMapByIds(goodsIdsReq);
        if (commodityMapByIds != null) {
    
    
            Map<String, GoodsVO> data = commodityMapByIds.getData();
            mapList.add(data);
        }
    }

}

Step 2: Specific use of thread classes in the service layer

     private Map<String, GoodsVO> getGoodsMap(List<AfterSalesDTO> afterSalesList) {
    
    
   
        List<String> goodIds = new ArrayList<>();
        for (AfterSalesDTO afterSalesDTO : afterSalesList) {
    
    
            List<AfterSalesItemDTO> itemDTOList = afterSalesDTO.getItems();
            if (CollectionUtils.isNotEmpty(itemDTOList)) {
    
    
                for (AfterSalesItemDTO afterSalesItemDTO : itemDTOList) {
    
    
                    goodIds.add(afterSalesItemDTO.getItemId());
                }
            }
        }
        goodIds = goodIds.stream().distinct().collect(Collectors.toList());
        List<Map<String, GoodsVO>> listMap = new ArrayList<>();
        listMap = Collections.synchronizedList(listMap);
        Integer countRow = goodIds.size();
        Integer times = 0;
        Integer perCount = 10000;
        if (countRow % perCount == 0) {
    
    
            times = countRow / perCount;
        } else {
    
    
            times = countRow / perCount + 1;
        }
        CountDownLatch countDownTimes = new CountDownLatch(times);
        List<String> innerList = new ArrayList<>();
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.initialize();
        executor.setCorePoolSize(3);
        for (int i = 1; i <= goodIds.size(); i++) {
    
    
            innerList.add(goodIds.get(i - 1));
            if (i % perCount == 0) {
    
    
                GoodsIdsReq goodsIdsReq = new GoodsIdsReq();
                goodsIdsReq.setCommodityIds(innerList);
                executor.execute(new GetCommodityThread(basicDataFeignClientService, countDownTimes, goodsIdsReq, listMap));
                innerList = new ArrayList<>();
                continue;
            }
            if (i % perCount != 0 && i == countRow) {
    
    
                GoodsIdsReq goodsIdsReq = new GoodsIdsReq();
                goodsIdsReq.setCommodityIds(innerList);
                executor.execute(new GetCommodityThread(basicDataFeignClientService, countDownTimes, goodsIdsReq, listMap));
            }
        }
        try {
    
    
            countDownTimes.await();
        } catch (InterruptedException e) {
    
    
            logger.info("InterruptedException occurs");
        }
        Map<String, GoodsVO> collect = listMap.stream()
                .map(Map::entrySet)
                .flatMap(Set::stream)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        return collect;
    }

Add some specific parameters, which can be written according to the actual situation

Guess you like

Origin blog.csdn.net/qq_32115447/article/details/115305006