Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService.submit(Task) vs CompletableFuture.supplyAsync(Task, Executor)

To run some stuff in parallel or asynchronously I can use either an ExecutorService: <T> Future<T> submit(Runnable task, T result); or the CompletableFuture Api:static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor); (Lets assume I use in both cases the same Executor)

Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what?

And what are the differences if I use the CompletableFuture API with default Executor (the method without executor)?

like image 678
dermoritz Avatar asked Sep 13 '16 14:09

dermoritz


People also ask

What is difference between ExecutorService and CompletableFuture?

The ExecutorService executes a task and updates the result in the Future. A CompletableFuture introduced later in Java 8 implements the Future interface. So CompletableFuture contains all the functionalities provided by the Future interface. The CompletableFuture allows you to chain tasks together.

What is the difference between executor and ExecutorService?

Executor just executes stuff you give it. ExecutorService adds startup, shutdown, and the ability to wait for and look at the status of jobs you've submitted for execution on top of Executor (which it extends). This is a perfect answer, short and clear.

What does ExecutorService submit do?

The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java. util. concurrent package.

What is difference between ExecutorService and ForkJoinPool?

The Fork/Join framework in Java 7 is an implementation of the Divide and Conquer algorithm, in which a central ForkJoinPool executes branching ForkJoinTasks. ExecutorService is an Executor that provides methods to manage the progress-tracking and termination of asynchronous tasks.


2 Answers

Besides the return type Future vs. CompletableFuture are there any remarkable differences. Or When to use what?

It's rather simple really. You use the Future when you want the executing thread to wait for async computation response. An example of this is with a parallel merge/sort. Sort left asynchronously, sort right synchronously, wait on left to complete (future.get()), merge results.

You use a CompleteableFuture when you want some action executed, with the result after completion, asynchronously from the executed thread. For instance: I want to do some computation asynchronously and when I compute, write the results to some system. The requesting thread may not need to wait on a result then.

You can mimic the above example in a single Future executable, but the CompletableFuture offers a more fluent interface with better error handling.

It really depends on what you want to do.

And what are the differences if i use the CompletableFutureApi with default Executor (the method without executor)?

It will delegate to ForkJoin.commonPool() which is a default size to the number of CPUs on your system. If you are doing something IO intensive (reading and writing to the file system) you should define the thread pool differently.

If it's CPU intensive, using the commonPool makes most sense.

like image 114
John Vint Avatar answered Oct 06 '22 00:10

John Vint


CompletableFuture has rich features like chaining multiple futures, combining the futures, executing some action after future is executed (both synchronously as well as asynchronously), etc.

However, CompletableFuture is no different than Future in terms of performance. Even when combine multiple instances of CompletableFuture (using .thenCombine and .join in the end), none of them get executed unless we call .get method and during this time, the invoking thread is blocked. I feel in terms of performance, this is not better than Future.

Please let me know if I am missing some aspect of performance here.

like image 20
Chinmay Phadke Avatar answered Oct 06 '22 00:10

Chinmay Phadke