I made a function that is called allOfSequentialSupplierCombine(...)
this function basically takes a linkedList
of Suppliers of promises, and then it executes the promises one by one by chaining them recursively and then it builds an array of the results.
I've been debugging the code, I used Jprofiler o profile the memory and nothing abnormal, i've been reading on how promises are handled in memory, and how thread pools work
private static <T> CompletionStage<List<T>> allOfSequentialSupplierCombine(LinkedList<Supplier<CompletionStage<T>>> chain) {
if (chain != null && !chain.isEmpty()) {
Supplier<CompletionStage<T>> future = chain.pop();
return future.get().thenCompose(r -> {
return allOfSequentialSupplierCombine(chain).thenApply(list -> {
List<T> result = list;
if (list == null) {
list = new ArrayList<>();
}
list.add(0, r);
return list;
});
});
}
return CompletableFuture.completedFuture(null);
}
The problem I am facing is that if I input more than 2000 suppliers, the function just stops without any error. I am expecting that maybe it creates too much threads in memory, resulting in the chain being broken.
Can anyone help me understand how the chained promises are stored in memory, does the jvm create as many threads as there are promises?