CompletableFuture multi-threaded, single thread concurrent, or both?

Viewed 5722

I just started looking into Java's CompletableFuture and a little bit confused on whether this is truly asynchronous (i.e running on one thread concurrently) or spanning multiple threads (Parallel).

For example, suppose I'd like to make 1000 different service calls. Suppose further that each service call can be made asynchronously. When using CompletableFuture, will the JVM make 1000 separate threads (assuming the JVM allows for this many threads), or execute all of these requests in one thread? Or is it doing a bit of both? Using some threads to execute those requests asynchronously?

What I want to do is something like this (in Python): https://pawelmhm.github.io/asyncio/python/aiohttp/2016/04/22/asyncio-aiohttp.html

is there a way to execute multiple requests/operations on the same thread in Java asynchronously?

3 Answers

Try to use CompletableFuture.thenCompose() to run multiple future sequentially in an async manner.

Related