#Completable Future

1 messages · Page 1 of 1 (latest)

lunar island
#

I am making around 30 api calls and I want to make these calls asynchronous and I am using Completable Future.

In the executor service I will only have 8 threads at a time.

Does anyone know if the 9th thread will wait until the thread is available or not?

Queue type - Synchronous Queue

supple flowerBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I am making around 30 api calls and I want to make these calls asynchronous. In the executor service I will only have 8 threads at a time. Does anyone know if the 9th thread will wait until the thread is available or not? Queue type - Synchronous Queue

be blocked until one of the first 8 threads is available?

Yes, in this scenario, the 9th thread will be blocked until one of the first 8 threads becomes available in the executor service. Since you have specified a limit of 8 threads, any additional tasks beyond that limit will have to wait until a thread is freed up before they can begin execution.

CompletableFuture can be used to make these API calls asynchronously and handle the results when they are ready. You can create a list of CompletableFutures for each API call and then use CompletableFuture.allOf() to wait for all of them to complete.

Here is an example code snippet:

List<CompletableFuture<Void>> futures = new ArrayList<>();

for (int i = 0; i < 30; i++) {
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        // Make API call here
    });

    futures.add(future);
}

CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

allOf.join(); // Wait for all futures to complete

This code will execute all 30 API calls asynchronously using CompletableFutures and wait for all of them to finish before proceeding.

light swallow
#

just do it like the bot showed and don't bother how it works under the hood

#

java will make sure to execute it as fast as possible

lunar island
#

Thanks @light swallow