CompletableFuture.supplyAsync 默认线程池
时间: 2023-11-25 15:52:18 浏览: 79
Java组合式异步编程CompletableFuture.pdf
是的,CompletableFuture.supplyAsync方法默认使用ForkJoinPool.commonPool()作为线程池。如果你想使用自定义的线程池,可以使用CompletableFuture.supplyAsync(Supplier<U> supplier, Executor executor)方法,并将自定义的线程池作为参数传入。下面是一个例子:
```java
Executor executor = Executors.newFixedThreadPool(3);
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
return "Hello World";
}, executor);
System.out.println(future.get()); // 输出:Hello World
```
阅读全文