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?





Are you asking about how to run myMethod in the executor, which is as simple as using thenComposeAsync​ instead of thenCompose,​ or are you asking about how to control what happens within myMethod regarding the returned future, which is impossible?
– Holger
Sep 20 at 13:53



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 CompletableFutures 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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)