ThreadPoolExecutor
also has beforeExecute
and afterExecute
hook methods that you can override and make use of. Here is the description from ThreadPoolExecutor
's Javadocs.
Hook methods
This class provides protected overridable
beforeExecute(java.lang.Thread, java.lang.Runnable)
andafterExecute(java.lang.Runnable, java.lang.Throwable)
methods that are called before and after execution of each task. These can be used to manipulate the execution environment; for example, reinitializingThreadLocals
, gathering statistics, or adding log entries. Additionally, methodterminated()
can be overridden to perform any special processing that needs to be done once theExecutor
has fully terminated. If hook or callback methods throw exceptions, internal worker threads may in turn fail and abruptly terminate.