Search code examples
javajava-threadsexecutor

Limiting Total Threads in Multiple Executors


I have a six processes that send data to external servers. Each process runs multiple "Transfer" threads to improve performance and paralellism (latencies, big files, etc.). I have one executor per process with 5 threads max each.

Now, since the external server supports only 20 threads, and my processes try to run 30 total threads (6 processes x 5 threads each), some threads crash. I get it.

Is there any way of creating a "big thread pool" (with 20 threads) in Java to limit the total transfer threads to a maximum of 20 for all processes?

Alternatively, I was thinking of creating a single executor for all processes but then one process could hog all threads, leaving the other ones starving.


Solution

  • You can use single executor and implement bulkhead pattern for your processes. Hystrix and Resilience4j have ready implementations, for example.