Java.util.concurrent.ThreadPoolExecutor

Source: Internet
Author: User

Java.util.concurrent.threadpoolexecutor-public class Threadpoolexecutor extends Abstractexecutorservice

How to create a thread: inherits the Thread class (Java.lang. thread-

public class Thread implements Runnable), implements the Runnable interface (Java.lang. runnable-

public interface Runnable)

There are 2 ways to achieve multithreading. But if the number of concurrent runs is high and the execution time of each thread is short, frequent thread creation can greatly reduce system efficiency because it takes time to create threads and destroy threads frequently. Then there is no way to make a thread reusable, that is, to perform a task is not destroyed, But can continue to perform other tasks? the thread pool can achieve this effect.

java.util.concurrent.threadpoolexecutor class is the core class in the thread pool , it has 4 constructors:

public class threadpoolexecutor extends abstractexecutorservice {     ...    public threadpoolexecutor (int corepoolsize,                                int maximumPoolSize,                                long keepAliveTime,                                TimeUnit unit,                                blockingqueue<rUnnable> workqueue)  {        this (corePoolSize,  maximumpoolsize, keepalivetime, unit, workqueue,              executors.defaultthreadfactory (),  defaulthandler);     }    public threadpoolexecutor (int corepoolsize,                                int maximumPoolSize,                                long keepAliveTime,                                timeunit unit,                               blockingqueue<runnable > workQueue,                               ThreadFactory  threadfactory)  {        this (Corepoolsize, maximumpoolsize,  keepAliveTime, unit, workQueue,              threadfactory, defaulthandler);     }    public  threadpoolexecutor (int corepoolsize,                                int maximumpoolsize,                               long keepalivetime,                                TimeUnit unit,                                BlockingQueue<Runnable> workQueue,                                rejectedexecutionhandler handler)   {        this (Corepoolsize, maximumpoolsize, keepalivetime,  unit, workqueue,              executors.defaultthreadfactory ( ),  handler);    }    /**     *  creates a new {@code  ThreadPoolExecutor} with the given initial      * parameters.     *     *  @param  corePoolSize the number of threads to keep in the  Pool, even     *        if they  are idle, unless {@code  allowCoreThreadTimeOut} is set      *  @param  maximumpoolsize the maximum number of threads to  allow in the     *         pool     *  @param  keepAliveTime when the number of threads is  Greater than     *        the core,  this is the maximum time that excess idle threads      *        will wait for new tasks  before terminating.     *  @param  unit the time  unit for the {@code  keepalivetime} argument     * @ param workqueue the queue to use for holding tasks before  they are     *        executed.   this queue will hold only the {@code  Runnable}      *        tasks submitted by the {@code  execute} method.      *  @param  threadfactory the factory to use when  the executor     *        creates  a new thread     *  @param  handler the handler  to use when execution is blocked     *         because the thread bounds and queue  capacities are reached     *  @throws  illegalargumentexception  if one of the following holds:<br>     *          {@code  corePoolSize < 0}<br>      *         {@code  keepAliveTime < 0}<br>      *         {@code  maximumpoolsize <=  0}<br>     *         {@code  maximumPoolSize < corePoolSize}     *  @throws   nullpointerexception if {@code  workQueue}     *          or {@code  threadfactory} or {@code  handler}  Is null     */    public threadpoolexecutor (int  corepoolsize,                               int maximumpoolsize,                                long keepAliveTime,                                TimeUnit unit,                                BlockingQueue<Runnable> workQueue,                                ThreadFactory threadFactory,                                rejectedexecutionhAndler handler)  {        if  (corepoolsize <  0 | |             maximumpoolsize <= 0  | |             maximumPoolSize <  corepoolsize | |             keepalivetime < 0)              throw new illegalargumentexception ();        if  (workqueue == null | |  threadfactory == null | |  handler == null)             throw  new nullpointerexception ();         this.corepoolsize =  corepoolsize;        this.maximumpoolsize = maximumpoolsize;         this.workqueue = workqueue;        this.keepalivetime =  unit.tonanos (KeepAliveTime);         this.threadfactory =  threadFactory;        this.handler = handler;  &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP, ...}

Constructor parameter implication:
Corepoolsize: Core pool size. After the thread pool is created, by default, threads are pooled and wireless, but wait until a task arrives to create the thread to perform the task, unless the Prestartcorethread () (public int) is called Prestartallcorethreads () {...}) or Prestartallcorethreads () (Public boolean prestartcorethread () {...}), which creates 1 threads or corepoolsize threads before a task arrives. By default, after the thread pool has been created, Thread pool threads = 0, after a task arrives, create 1 threads to execute, and when the number of threads in the thread pool reaches corepoolsize, the incoming task is placed in the cache queue
Maximumpoolsize: The maximum number of threads in a thread pool, representing the maximum number of threads that can be created in a threaded pool
KeepAliveTime: Indicates how long the thread will be terminated without a task execution. By default, KeepAliveTime only works until the number of threads in the thread pool >corepoolsize, until the number of threads in the thread pool <= Corepoolsize. But if the Allowcorethreadtimeout (Boolean) (Public void Allowcorethreadtimeout (Boolean value) {...}) is called, The number of threads in the thread pool <=corepoolsize when KeepAliveTime also works until the number of threads in the thread pool = 0
Unit:keepalivetime time unit, there are 7 kinds (java.util.concurrent.timeunit-public enum timeunit):
Timeunit.nanoseconds
Timeunit.microseconds
Timeunit.milliseconds
Timeunit.seconds
Timeunit.minutes
Timeunit.hours
Timeunit.days

This article is from the "SUNSHINE" blog, make sure to keep this source http://cangyu2013.blog.51cto.com/8493070/1769466

Java.util.concurrent.ThreadPoolExecutor

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.