29 Ocak 2019 Salı

CompletableFuture.thenApplyAsync metodu

Giriş
Bu metodun bir eksikliğinin açıklaması şöyle.
By default, all *Async composition methods of CompletableFutrure use ForkJoinPool.commonPool() (see here) unless the explicit Executor is specified. This thread pool is shared between all CompletableFuture-s and all parallel streams across all applications deployed on the same JVM. This hard-coded, unconfigurable thread pool is completely outside of application developers' control, hard to monitor and scale. Therefore, in robust real-life applications, you should always specify your own Executor. With API enhancements in Java 9+, you can fix this drawback, but it will require some custom coding.
thenApplyAsync metodu
ForkJoinPool içinde çalışır.
Örnek
Şöyle yaparız
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 10)
        .thenApplyAsync(result -> result * 2)
        .thenApplyAsync(result -> result + 5);

future.thenAccept(result -> System.out.println(result));
thenApplyAsync metodu - Executor
Belirtilen Executor içinde çalışır.

Hiç yorum yok:

Yorum Gönder