1

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.

The Impaler
  • 45,731
  • 9
  • 39
  • 76

1 Answers1

2

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

Alexander Pankin
  • 3,787
  • 1
  • 13
  • 23
  • Seems exactly what I'm looking for. I assume Java doesn't offer any standard implementation, so I'll need to implement it, right? – The Impaler Oct 11 '18 at 17:52
  • You can implement it with standart Java objects like locks or semaphores or atomic counters, or you can use 3rd party library. Depends on your tasks and preferences. – Alexander Pankin Oct 11 '18 at 17:55
  • Great, I think I'll use a single executor instead of 6, and will use a semaphore for each process (with size 5). – The Impaler Oct 11 '18 at 17:57