Resize ThreadPoolExecutor Queue

I need to resize ThreadPoolExecutor task queue. Of course, BlockingQueue does not support resizing, and ThreadPoolExecutor does not support resizing.

So, the method I came up with is to use ThreadPoolExecutor.shutdownNow () , which returns me a list of Runnables that have not yet been executed. Then I can create a new artist with the desired queue size and resubmit all tasks.

The problem is related to the tasks being performed during the shutdownNow () call. As far as I can tell from javadoc, the executor will call Thread.interrupt () for all threads currently executing taks. I do not want my tasks to be killed. This question may have been a long way to ask, how do I write my tasks so that Thread.interrupt () has no effect?

+6
source share
2 answers

Using the combination "shutdown ()" (not shutdownNow ()), then polling with getPoolSize () / isTerminated (), you can (a) stop the existing pool. Then (b) at the same time (in a separate thread), a new queue can be created with the desired size. You will have a trade-off here in terms of: can you let more threads temporarily exist there than the desired number (while the first pool is disconnected).

+3
source

You must use the isTerminated () method to check the status of the Executor, but before calling the isTerminated method you must call shutdown () otherwise the isTermainated aleays method returns false.

0
source

All Articles