Spring's threadpooltaskexecutor class is ultimately implemented by calling Java's threadpoolexecutor's void execute (runnable task) method or future <?> Submit (runnable task) Method
Below are the inheritance levels of spring's task execution class and Interface
Interface executor
Void execute (runnable command );
Interface taskexecutor extends executor
Void execute (runnable task );
Interface asynctaskexecutor extends taskexecutor
Void execute (runnable task, long starttimeout );
Future <?> Submit (runnable task );
<T> future <t> submit (callable <t> task );
Interface schedulingtaskexecutor extends asynctaskexecutor
Boolean prefersasklivedtasks ();
Task execution class
Class threadpooltaskexecutor extends executorconfigurationsupport implements schedulingtaskexecutor
Member variables
Private threadpoolexecutor;
Task execution Method
Public void execute (runnable task ){
Executor executor = getthreadpoolexecutor ();
Try {
Executor.exe cute (task );
}
Catch (rejectedexecutionexception ex ){
Throw new taskrejectedexception ("executor [" + executor + "] did not accept task:" + task, ex );
}
}
Public threadpoolexecutor getthreadpoolexecutor () throws illegalstateexception {
Assert. State (this. threadpoolexecutor! = NULL, "threadpooltaskexecutor not initialized ");
Return this. threadpoolexecutor;
}
Base Class executorconfigurationsupport
Abstract class executorconfigurationsupport extends customizablethreadfactory
Implements beannameaware, initializingbean, disposablebean
The basic class customizablethreadfactory is the custom thread factory class.
Member variables
Private executorservice executor;
Lifecycle Initialization
Public void afterpropertiesset (){
Initialize ();
}
Initialization Method
/**
* Set up the executorservice.
*/
Public void initialize (){
If (logger. isinfoenabled ()){
Logger.info ("Initializing executorservice" + (this. beanname! = NULL? "'" + This. beanname + "'":""));
}
If (! This. threadnameprefixset & this. beanname! = NULL ){
Setthreadnameprefix (this. beanname + "-");
}
This.exe cutor = initializeexecutor (this. threadfactory, this. rejectedexecutionhandler );
}
Abstract method (implementation of threadpooltaskexecutor)
Protected abstract executorservice initializeexecutor (
Threadfactory, rejectedexecutionhandler );
Lifecycle destruction method
Public void destroy (){
Shutdown ();
}
Threadpooltaskexecutor implements executorservice initializeexecutor (threadfactory, rejectedexecutionhandler); Method
Protected executorservice initializeexecutor (
Threadfactory, rejectedexecutionhandler ){
Blockingqueue <runnable> queue = createqueue (this. queuecapacity );
Threadpoolexecutor executor = new threadpoolexecutor (
This. corepoolsize, this. maxpoolsize, this. keepaliveseconds, timeunit. Seconds,
Queue, threadfactory, rejectedexecutionhandler );
If (this. allowcorethreadtimeout ){
Executor. allowcorethreadtimeout (true );
}
This. threadpoolexecutor = executor;
Return executor;
}
We noticed that the above method initializeexecutor (threadfactory, rejectedexecutionhandler) was called by the parent class executorconfigurationsupport and initialized the parent class member variable private executorservice executor;
Threadpooltaskexecutor uses its own member variable private threadpoolexecutor;
Public threadpoolexecutor getthreadpoolexecutor () throws illegalstateexception {
Assert. State (this. threadpoolexecutor! = NULL, "threadpooltaskexecutor not initialized ");
Return this. threadpoolexecutor;
}
I don't understand why I want to do this here?
Look at other parts
Executorconfigurationsupport
Private rejectedexecutionhandler = new threadpoolexecutor. abortpolicy ();
Threadpooltaskexecutor member variable
Private int corepoolsize = 1;
Private int maxpoolsize = integer. max_value; // default maximum Thread Pool
Private int keepaliveseconds = 60;
Private Boolean allowcorethreadtimeout = false;
Private int queuecapacity = integer. max_value; // default queue capacity
Blocking queue Creation Method
Protected blockingqueue <runnable> createqueue (INT queuecapacity ){
If (queuecapacity> 0 ){
Return new queue blockingqueue <runnable> (queuecapacity );
}
Else {
Return new synchronousqueue <runnable> ();
}
}