0

I have a parallel stream with a few database queries inside like this:

private void processParallel() {
List\<Result\> = objects.parallelStream().peek(object -\> {
doSomething(object)
}
}

private void doSomething(object) {
CompletableFuture<String> param =
        CompletableFuture.supplyAsync(() -> objectService.getParam(object.getField())),
                executor)
                .thenApply(object-> Optional.ofNullable(object)
                        .map(Object::getParam)
                        .orElse(null));
}

I need to specify the pool size, but setting the property "java.util.concurrent.ForkJoinPool.common.parallelism","20" is not working, probably because of locking the stream. Is there any way to limit the max amount of threads?

  • The way you've designed your code, the number of threads is not a cap on the number of concurrent requests. Limiting the threads isn't what you need, you need, like, to use a Semaphore or something to cap the concurrency. – Louis Wasserman Nov 07 '22 at 18:35

1 Answers1

0

Since parallel streams are using Fork/Join framework under the hood, to limit the number of treads employed by the stream, you can wrap the stream with a Callable and define a ForkJoinPool having the required level of parallelism as described.

The threads occupied by the parallel Stream would be taken from the new created ForkJoinPool, to which the callable task was submitted (not from the common poll) as described here.

The downside of this approach is that you're relying on the implementation detail of the Stream API.

And also as @Louis Wasserman has pointed out in the comment you probably might need another way of limiting the number of threads used in the stream. Because you're performing database queries, each tread would require a database connection to do its job, hence the number of threads should not be greater than the number of available connections that the data source can provide at the moment. And if you have multiple processes that can fire these asynchronous tasks simultaneously (for instance in a web application), it doesn't seem to be a good idea to try to develop your own solution. If that's the case, you might consider using a framework like Spring WebFlax.

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46