Search code examples
javaconcurrencyjava.util.concurrent

In java, how to process items off a bounded queue with at most N threads, with adjustable N?


Let's say in Java I have a bounded BlockingQueue and I want to process items off that queue with at most N threads. The amount N should be adjustable. The solution should take not more than N items off the queue, such that it preserves its role as providing back pressure. How would I achieve this?

I got a long way with http://jcip.net/listings/BoundedExecutor.java, but that doesn't really allow for resizes.


Solution

  • Maybe you can upgrade the class http://jcip.net/listings/BoundedExecutor.java to provide a function which can add more permits to Semaphore (you can use release(int permits) to add more permits) and use CachedThreadPool which will provide the flexibility you needed?