Unlike stated on some blogs (for example, I can’t emphasize this enough: the thenAccept () / thenRun () methods do not block ) CompletableFuture.thenAccept can really be blocked. Consider the following code, uncommenting a call to the pause method will cause a thenAccept lock:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { log.trace("return 42"); return "42"; }); //pause(1000); //uncommenting this will cause blocking of thenAccept future.thenAccept((dbl -> { log.trace("blocking"); pause(500); log.debug("Result: " + dbl); })); log.trace("end"); pause(1000);
Can we be sure that the following will not be blocked? I understand that if supplyAsync starts immediately, then thenAccept may block, no?
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> { return "42"; }).thenAccept((dbl -> { pause(500); log.debug("Result: " + dbl); }));
source share