| | |
| | | import java.net.URL; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.*; |
| | | import java.util.stream.Collectors; |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | public ServerResponse getStockByType(int pageNum, int pageSize, String orderBy, String keyWords, String stockType, HttpServletRequest request) { |
| | | List<Stock> stockList = new ArrayList<>(); |
| | | PageHelper.startPage(pageNum, pageSize); |
| | | stockList.addAll(stockMapper.findStockByType(orderBy,stockType,keyWords)); |
| | | stockList.addAll(stockMapper.findStockByType(orderBy, stockType, keyWords)); |
| | | List<StockListVO> stockListVOS = Lists.newArrayList(); |
| | | if (stockList.size() > 0){ |
| | | if (stockList.size() > 0) { |
| | | stockListVOS.addAll(Objects.requireNonNull(StockApi.getStockReailTimes(stockList))); |
| | | } |
| | | for (int i = 0; i <stockListVOS.size() ; i++) { |
| | | stockListVOS.get(i).setNowPrice(iPriceServices.getNowPrice(stockListVOS.get(i).getCode(),stockType).toString()); |
| | | } |
| | | |
| | | // 根据实际情况调整核心线程数和最大线程数 |
| | | 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()); |
| | | |
| | | // 等待所有CompletableFuture完成 |
| | | CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); |
| | | |
| | | |
| | | |
| | | RPageInfo pageInfo = new RPageInfo(); |
| | | pageInfo.setList(stockListVOS); |
| | | pageInfo.setStockType(stockType); |
| | | return ServerResponse.createBySuccess(pageInfo); |
| | | } |
| | | |
| | | |
| | | public ServerResponse getDateline(HttpServletResponse response, String code) { |
| | | if (StringUtils.isBlank(code)) |
| | |
| | | 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()); |