hippo4j
hippo4j copied to clipboard
Added a new thread pool plugin that allows users to actively destroy worker threads (#1208)
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 using setOverflowThreadNumber
or updated using checkOverflowThreads
based on activeCount - maximumActiveCount
.
When the worker
in the thread pool executes the beforeExecute
method, it checks the overflowThreadNumber
. If there are overflow threads, it will atomically decrement the value using CAS, output a warning log, throw an IllegalMaximumActiveCountException
exception, and interrupt the worker
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.