Overview
Threadpoolexecutor as a java.util.concurrent package to provide the basis for external implementation, in the form of internal thread pool to provide management task execution, thread scheduling, thread pool management and other services.
Construction parameters
Corepoolsize the number of core threads. Maximumpoolsize the maximum number of threads. KeepAliveTime Maximum survival time. Unit time units. Workqueue blocks the task queue. Threadfactory new Thread Factory. Handler when the number of submitted tasks exceeds the sum of Maxmumpoolsize+workqueue, the task is assigned to Rejectedexecutionhandler to handle.
1. When the thread pool is less than corepoolsize, the new commit task creates a new thread to perform the task, even if there are idle threads in the thread pools.
2. When the thread pool reaches corepoolsize, the new commit task is put into Workqueue, waiting for the task to be scheduled to execute in the thread pool
3. When Workqueue is full and maximumpoolsize>corepoolsize, the new commit task creates a new thread to perform the task
4. When the number of submitted tasks exceeds maximumpoolsize, the new submission task is handled by Rejectedexecutionhandler
5. When the thread pool exceeds the corepoolsize thread and the idle time reaches KeepAliveTime, the idle thread is closed
6. When setting Allowcorethreadtimeout (true), corepoolsize thread idle time in the thread pool reaches KeepAliveTime will also close
PublicThreadpoolexecutor (intCorepoolsize,intMaximumpoolsize,LongKeepAliveTime, timeunit unit, Blockingqueue<Runnable>WorkQueue, Threadfactory threadfactory, Rejectedexecutionha Ndler handler) {if(Corepoolsize < 0 | |maximumpoolsize<= 0 | |maximumpoolsize< Corepoolsize | |KeepAliveTime< 0) Throw Newillegalargumentexception (); if(WorkQueue = =NULL|| Threadfactory = =NULL|| Handler = =NULL) Throw NewNullPointerException (); This. corepoolsize =corepoolsize; This. maximumpoolsize =maximumpoolsize; This. WorkQueue =WorkQueue; This. KeepAliveTime =Unit.tonanos (KeepAliveTime); This. Threadfactory =threadfactory; This. Handler =handler; }
Java and the threadpoolexecutor of the contract