1
zj
2024-06-04 bdb109fc4f6d9af242380c1f49f76fad2247b496
src/main/java/com/nq/service/impl/StockServiceImpl.java
@@ -36,9 +36,7 @@
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@@ -178,16 +176,20 @@
            stockListVOS.addAll(Objects.requireNonNull(StockApi.getStockReailTimes(stockList)));
        }
        // Use CompletableFuture to perform asynchronous processing for each stockListVO
        ExecutorService executor = Executors.newFixedThreadPool(20); // Create a fixed thread pool with a maximum of 10 threads
        // 根据实际情况调整核心线程数和最大线程数
        int corePoolSize = Runtime.getRuntime().availableProcessors(); // 根据实际情况设置核心线程数(通常为CPU核心数)
        int maxPoolSize = corePoolSize * 2; // 设置最大线程数为核心线程数的两倍
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
        List<CompletableFuture<Void>> futures = stockListVOS.stream()
                .map(stockListVO -> CompletableFuture.supplyAsync(() ->
                                iPriceServices.getNowPrice(stockListVO.getCode(), stockType), executor)
                        .thenAcceptAsync(price -> stockListVO.setNowPrice(price.toString()), executor))
                .collect(Collectors.toList());
        // Wait for all futures to complete
        // 等待所有CompletableFuture完成
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        RPageInfo pageInfo = new RPageInfo();
        pageInfo.setList(stockListVOS);
@@ -276,6 +278,8 @@
        String introduction = null;
        StockVO   stockVO = StockApi.assembleInStockVO(stock);
        stockVO.setDepositAmt(depositAmt);
        BigDecimal price = stockDzMapper.getPrice(stock.getStockCode());
        stockVO.setPrice(price != null ? price.toString() : "0");
        stockVO.setNowPrice(iPriceServices.getNowPrice(stock.getStockCode(),stock.getStockType()).toString());
        stockVO.setType(stock.getStockType());
        stockVO.setId(stock.getId().intValue());