int availableProcessors = Runtime.getRuntime().availableProcessors();
int corePoolSize = availableProcessors * 2; // 根据需要进行调整和优化
int maximumPoolSize = availableProcessors * 4; // 根据需要进行调整和优化
ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.MILLISECONDS
, new LinkedBlockingQueue<>(1024)
, new ThreadPoolExecutor.AbortPolicy());
int size = nodeList.size();
// 每个批次的大小,至少为1
int batchSize = Math.max(size / corePoolSize, 1);
// 批次的数量
int numBatches = (int) Math.ceil((double) size / batchSize);
CompletableFuture<Void>[] futures = new CompletableFuture[numBatches];
for (int i = 0; i < numBatches; i++) {
int startIndex = i * batchSize;
int endIndex = Math.min(startIndex + batchSize, size);
List<SourceDataTreeNode> batch = nodeList.subList(startIndex, endIndex);
futures[i] = CompletableFuture.runAsync(() -> {
getInnermostData(batch, platform, searchName);
}, executor);
}
CompletableFuture.allOf(futures).get();
根据电脑cpu创建线程池
于 2023-10-20 15:03:16 首次发布