Added a new thread pool plugin that allows users to actively destroy worker threads (#1208) #1362
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1208
Changes proposed in this pull request:
A new plugin
The plugin uses the method of throwing exceptions to let the thread pool actively interrupt (destroy) worker threads without losing tasks.
This plugin extends the
ExecuteAwarePlugin
interface and is used to check if the number of active threads in the thread pool exceeds the maximum thread count.Internally, it maintains an
overflowThreadNumber
variable to represent the number of overflow threads (i.e.,activeCount - maximumActiveCount
). This variable can be set usingsetOverflowThreadNumber
or updated usingcheckOverflowThreads
based onactiveCount - maximumActiveCount
.When the
worker
in the thread pool executes thebeforeExecute
method, it checks theoverflowThreadNumber
. If there are overflow threads, it will atomically decrement the value using CAS, output a warning log, throw anIllegalMaximumActiveCountException
exception, and interrupt theworker
thread. Finally, it resubmits the task to the current thread pool.A new plugin registrar
For performance reasons, if the
overflowThreadNumber
is not explicitly set to a non-zero value, it will not actively check the thread pool.We also provide a
MaximumActiveThreadCountCheckerRegistrar
for managing this plugin. In addition to registering the plugin with the thread pool using it, it also supports periodically initiating checks to thread pools registered with this plugin.