Looks like it runs in the same executor as its CompletionStage
public static void main(String[] args) throws Exception {
//ExecutorService executorService = Executors.newFixedThreadPool(3);
ExecutorService executorService = Executors.newWorkStealingPool();
while (true) {
int i = Instant.now().getNano();
CompletableFuture<?> completableFuture = CompletableFuture.supplyAsync(
()-> {
System.err.printf("async thread at %d -> %s\n", i, Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
throw new RuntimeException();
}, executorService);
completableFuture.exceptionally(
(err)-> {
System.err.printf("error thread for %d -> %s\n", i, Thread.currentThread().getName());
return null;
});
TimeUnit.SECONDS.sleep(5);
}
}
For fixed size:
async thread at 418000000 -> pool-1-thread-1
error thread for 418000000 -> pool-1-thread-1
async thread at 646000000 -> pool-1-thread-2
error thread for 646000000 -> pool-1-thread-2
async thread at 646000000 -> pool-1-thread-3
error thread for 646000000 -> pool-1-thread-3
async thread at 646000000 -> pool-1-thread-1
error thread for 646000000 -> pool-1-thread-1
async thread at 647000000 -> pool-1-thread-2
error thread for 647000000 -> pool-1-thread-2
For stealing pool(4 cores):
async thread at 96000000 -> ForkJoinPool-1-worker-1
error thread for 96000000 -> ForkJoinPool-1-worker-1
async thread at 196000000 -> ForkJoinPool-1-worker-1
error thread for 196000000 -> ForkJoinPool-1-worker-1
async thread at 197000000 -> ForkJoinPool-1-worker-1
error thread for 197000000 -> ForkJoinPool-1-worker-1
async thread at 197000000 -> ForkJoinPool-1-worker-1
error thread for 197000000 -> ForkJoinPool-1-worker-1
async thread at 197000000 -> ForkJoinPool-1-worker-1
error thread for 197000000 -> ForkJoinPool-1-worker-1
async thread at 197000000 -> ForkJoinPool-1-worker-1
with no executor:
async thread at 848000000 -> ForkJoinPool.commonPool-worker-1
error thread for 848000000 -> ForkJoinPool.commonPool-worker-1
async thread at 944000000 -> ForkJoinPool.commonPool-worker-1
error thread for 944000000 -> ForkJoinPool.commonPool-worker-1
async thread at 944000000 -> ForkJoinPool.commonPool-worker-1
error thread for 944000000 -> ForkJoinPool.commonPool-worker-1
async thread at 944000000 -> ForkJoinPool.commonPool-worker-1
error thread for 944000000 -> ForkJoinPool.commonPool-worker-1
async thread at 944000000 -> ForkJoinPool.commonPool-worker-1
error thread for 944000000 -> ForkJoinPool.commonPool-worker-1