Java CompletableFuture assigning executor
Java CompletableFuture assigning executor
I am having confusion with defining executor in CompletableFuture. I am not sure how to tell CompletableFuture to run it in that particular executor. Thanks in advance.
//Suppose I have an executor
ExecutorService myExecutor=Executors.newFixedThreadPool(2);
//If I create a future like this
CompletableFuture.runAsync(() ->
//Do something
, myExecutor); // I can put the executor here and say the future to this executor
//But I do not know where to put executor if I create my future in method style like this
private final CompletableFuture<Void> myMethod(String something)
//Do something
return null;
//and use it like this
.thenCompose(this::myMethod); //How can I specify the executor in this case?
myMethod
thenComposeAsync
thenCompose
myMethod
2 Answers
2
you can do something like this:
ExecutorService es = Executors.newFixedThreadPool(4);
List<Runnable> tasks = getTasks();
CompletableFuture<?> futures = tasks.stream()
.map(task -> CompletableFuture.runAsync(task, es))
.toArray(CompletableFuture::new);
CompletableFuture.allOf(futures).join();
es.shutdown();
This is interesting. Is this the only way? The OP seems to be trying to make a composite kind of
CompletableFuture
first, then submit it. Whereas you make the individual CompletableFutures
first, then combine them into a larger "virtual" future object. Is the OP's way viable? (It didn't seem to be when I looked at the API.)– markspace
Sep 2 at 18:38
CompletableFuture
CompletableFutures
In your example, you have 3 CompletableFuture
s that are at play:
CompletableFuture
runAsync()
myMethod()
thenCompose()
You also have 4 tasks that need to be run:
runAsync()
myMethod()
thenCompose()
thenComposeAsync()
myMethod()
myMethod()
thenCompose()
myMethod()
As you can see, several tasks and executors are involved, but you can always control the executors used in dependent stages using *Async()
variants. The only case where you don't really control it is the 4th case, but it is a cheap operation as long as dependent stages use the *Async()
variants as well.
*Async()
*Async()
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you asking about how to run
myMethod
in the executor, which is as simple as usingthenComposeAsync
instead ofthenCompose
, or are you asking about how to control what happens withinmyMethod
regarding the returned future, which is impossible?– Holger
Sep 20 at 13:53