From 4f0c87bbb84270e7b1dc3bd8dc1a4aa7ced128e7 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Mon, 03 Jun 2024 18:19:05 +0800
Subject: [PATCH] 1

---
 src/main/java/com/nq/service/impl/StockServiceImpl.java |   14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/nq/service/impl/StockServiceImpl.java b/src/main/java/com/nq/service/impl/StockServiceImpl.java
index 1c7c7a2..40c79f9 100644
--- a/src/main/java/com/nq/service/impl/StockServiceImpl.java
+++ b/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,17 +176,21 @@
             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);
         pageInfo.setStockType(stockType);

--
Gitblit v1.9.3