| | |
| | | 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; |
| | |
| | | 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); |