Spring parses the Java thread pool encapsulation source code

Source: Internet
Author: User

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> ();
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.