October 27, 2024
Chicago 12, Melborne City, USA
java

How to limit number of virtual threads to avoid large number of DB connections?


I am currently using Executors.newFixedThreadPool to run a large number of tasks in parallel.

try(ExecutorService executorService = Executors.newFixedThreadPool(10)) {
        List<Future<Integer>> resultFutures = executorService.invokeAll(tasks);
        for (Future<Integer> rf: resultFutures ) {
                    ....
         }
         executorService.shutdown();
 }

Each task opens a DB connection.

I want to use virtual threads for same(Executors.newVirtualThreadPerTaskExecutor()).

But it may lead to large number of DB connections.

I searched, but couldn’t find a way to limit number of virtual threads.

What is a better way of dealing with this situation?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video