Skip to main content


Showing posts from November, 2015

Which thread executes CompletableFuture's tasks and callbacks?

CompletableFuture is still a relatively fresh concept, despite being introduced almost two years ago (!) in March 2014 with Java 8. But maybe it's good that this class is not so well known since it can be easily abused, especially with regards to threads and thread pools that are involved along the way. This article aims to describe how threads are used with CompletableFuture . Running tasks This is the fundamental part of the API. There is a convenient supplyAsync() method that is similar to ExecutorService.submit() , but returning CompletableFuture : CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> { try (InputStream is = new URL("").openStream()) {"Downloading"); return IOUtils.toString(is, StandardCharsets.UTF_8); } catch (IOException e) { throw new RuntimeException(e); } }); The proble