How to install ForkJoinPool with the required number of workflows in the CompletableFuture.supplyAsync application (Provider <U>)?

According to Oracle,

static Completed Future supplyAsync (Supplier Provider) Returns a new CompletableFuture, which asynchronously completes the task executed in ForkJoinPool.commonPool () with the obtained value by calling this Supplier.

static Completed Future supplyAsync (Supplier Provider, Contractor Executor) Returns a new CompletableFuture that is asynchronously completed by a job executed in this executor with the value obtained by calling this Supplier.

If I use the CompletedFuture static supplyAsync static method , use ForkJoinPool.commonPool () by default. This returns ForkJoinPool, which has the number of worker threads equal to the number of available cores in the running machine.

But I want to use ForkJoinPool with my custom number of workflows. Using ForkJoinPool.commonPool (), I cannot do this.

So, how can I use the CompletableFuture.supplyAsync method with my declared ForkJoinPool using the number of workflows I want?

+6
source share
1 answer

ForkJoinPool implements Executor .

Therefore, you can write your code as follows:

 int threadCount = 3; ForkJoinPool myPool = new ForkJoinPool(threadCount); CompletableFuture cf = CompletableFuture.supplyAsync(mySup, myPool); 
+7
source

All Articles