Java Multithreading High Concurrency learning Note (iii)--in-depth understanding of thread pool

Source: Internet
Author: User

The core of the thread pool is a class: Threadpoolexecutor.

Take a look at the constructor for this class:

 PublicThreadpoolexecutor (intParamInt1,intParamInt2,LongParamlong, Timeunit paramtimeunit, Blockingqueue<Runnable>paramblockingqueue) {         This(ParamInt1, ParamInt2, Paramlong, Paramtimeunit, Paramblockingqueue, Executors.defaultthreadfactory (),    DefaultHandler); }     PublicThreadpoolexecutor (intParamInt1,intParamInt2,LongParamlong, Timeunit paramtimeunit, Blockingqueue<Runnable>Paramblockingqueue, Threadfactory paramthreadfactory) {         This(ParamInt1, ParamInt2, Paramlong, Paramtimeunit, Paramblockingqueue, Paramthreadfactory, DefaultHandler); }     PublicThreadpoolexecutor (intParamInt1,intParamInt2,LongParamlong, Timeunit paramtimeunit, Blockingqueue<Runnable>Paramblockingqueue, Rejectedexecutionhandler Paramrejectedexecutionhandler) {         This(ParamInt1, ParamInt2, Paramlong, Paramtimeunit, Paramblockingqueue, Executors.defaultthreadfactory (),    Paramrejectedexecutionhandler); }     PublicThreadpoolexecutor (intParamInt1,intParamInt2,LongParamlong, Timeunit paramtimeunit, Blockingqueue<Runnable>Paramblockingqueue, Threadfactory paramthreadfactory, Rejectedexecutionhandler Paramrejectedexecutionhandl ER) { This. CTL =NewAtomicinteger (Ctlof (-536870912, 0));  This. Mainlock =NewReentrantlock ();  This. Workers =NewHashSet ();  This. termination = This. Mainlock.newcondition (); if((ParamInt1 < 0) | | (paramInt2 <= 0) | | (ParamInt2 < paramInt1) | | (Paramlong < 0L))            Throw Newillegalargumentexception (); if(Paramblockingqueue = =NULL) || (Paramthreadfactory = =NULL) || (Paramrejectedexecutionhandler = =NULL))            Throw NewNullPointerException ();  This. corepoolsize =ParamInt1;  This. maximumpoolsize =ParamInt2;  This. WorkQueue =Paramblockingqueue;  This. KeepAliveTime =Paramtimeunit.tonanos (Paramlong);  This. Threadfactory =paramthreadfactory;  This. Handler =Paramrejectedexecutionhandler; }

Corepoolsize: The core pool size of the thread pool, after the thread pool is created, the thread pool does not have any threads by default.

just go back when there's a mission. Create a thread to perform the task. To put it another way, after the thread pool is created, the number of threads in the threads pools is 0, and when the task is created a thread executes until the number of threads reaches corepoolsize , and the task that is reached is placed in the queue. (Note that it is the task that arrives). In other words:corepoolsize represents the maximum number of threads allowed to run concurrently in a thread pool.

Maximumpoolsize: The maximum number of threads allowed by the thread pool, which indicates the maximum number of threads that can be created. maximumpoolsize is certainly greater than corepoolsize .

KeepAliveTime: Indicates how long the thread does not have a task to hold and then stops. By default, KeepAliveTime only works if the thread pool hasmore threads than Corepoolsize . In other words, when the number of threads in the thread pool is greater than corepoolsize , and a thread's idle time reaches KeepAliveTime , it is shutdown.

the parameters of the unit:keepalivetime.

WorkQueue : A blocking queue that is used to store tasks waiting to be executed, which is important in the thread pool.

Arrayblockingqueue and priorityblockingqueue use less, generally using linkedblockingdeque and synchronousqueue,

the thread pool's permutation strategy and Blockingqueue about.

Threadfactory : Thread factory, used to create threads.

Handler: represents the policy when a task is denied processing.

AbortPolicy: discards the task and throws rejectedexecutionexception

Callerrunspolicy: There is a calling thread to handle the task

Discardoldestpolicy: Discards the first task in the queue and then re-executes the task

Discardpolicy: Discard the task without throwing an exception

Java Multithreading High Concurrency learning Note (iii)--in-depth understanding of thread pool

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.