CompletableFuture 异步处理

CompletableFuture 异步处理

private static final ExecutorService threadPool = Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());

LocalDate firstDate = LocalDate.of(2022, 9, 1);
LocalDate today = LocalDate.now();
//计算日期间隔天数
long days = ChronoUnit.DAYS.between(firstDate, today);


HashSet<LocalDate> daySet = new LinkedHashSet<>(64);

for (int i = 0; i < days; i++) {
    LocalDate day = firstDate.plusDays(i);
    daySet.add(day);
}

daySet.stream().map(day -> CompletableFuture.runAsync(() -> {
    //耗时事件、异步处理流程 TODO
}, threadPool))
.collect(Collectors.toList())
.stream().map(CompletableFuture::join)
.collect(Collectors.toList());

你可能感兴趣的:(工具类,前端,javascript,开发语言)