What is the advantage of passing code directly to thread vs using CompletableFuture instead?
Thread thread = new Thread(() -> {do something});
thread.start();
VS
CompletableFuture<Void> cf1 =
CompletableFuture.runAsync(() -> {do something});
CompletableFuture.runAsync(...)
runs the Runnable in the forkJoin-Pool which is managed, whilenew Thread()
creates a new thread which you have to manage.What does "is managed" mean, it's pre-allocated and the threads are shared in the JVM. When the runnable is completed, the thread can be reused for other runnables. This makes better usage of resources, especially as thread instantiation is an expensive operation - not only the object, but also some extra non-heap memory - the thread stack - has to be allocated.
@Gerald Mücke already mentioned the important difference:
CompletableFuture will use threads managed by a ThreadPool (default or customized).
However, I think the following two points are also should be considered.
First
CompletableFuture has so many easily understandable methods to chain different asynchronous computations together, making it much easier to introduce asynchrony than directly using Thread.
Second
You should never forget to handle exceptions; with CompletableFuture, you can directly handle them like this:
or take advantage of them this way to interrupt the computation:
while you will easily encounter some serious problems using Thread.