I need to be able to change the size of the task queue of a ThreadPoolExecutor. Of course, the BlockingQueue doesn't support changing the size, and the ThreadPoolExecutor doesn't support changing the queue.
So, the method I've come up with is to use ThreadPoolExecutor.shutdownNow(), which gives me back a list of the Runnables that haven't yet been executed. Then I can create a new executor with the desired queue size and re-submit all of the tasks.
The issue is with the tasks in progress at the time of the shutdownNow() call. As far as I can tell from the javadoc, the executor will call Thread.interrupt() on all threads currently executing a taks. I don't want my tasks to be killed. This question may have been a long-winded way of asking how to write my tasks such that Thread.interrupt() won't have any effect?
Using a mix of "shutdown()" (not shutdownNow()), then polling with getPoolSize()/isTerminated(), you could (a) stop the existing the pool . Then (b) simultaneously (in a separate thread) a new queue with the desired size could be created. You would have a tradeoff here in terms of: can you allow there to temporarily exist more threads than the desired number (while the first pool is shutting down).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With