`

ThreadPoolExecutor线程池参数详解

 
阅读更多
参考:https://www.cnblogs.com/waytobestcoder/p/5323130.html
https://www.cnblogs.com/bqcoder/p/6089101.html CountDownLatch
//单例线程池一
package ddd;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool {
	
	private static ThreadPoolExecutor te;
	
	// 核心线程池大小(线程池初始大小);
	private static int SHOPPINGCOREPOOLSIZE = 50;
	// 最大线程池大小;
	private static int SHOPPINGMAXIMUMPOOLSIZE = 50;
	// 线程池中超过corePoolSize数目的空闲线程最大存活时间;
	private static int SHOPPINGKEEPALIVETIME = 5;
	// 线程池队列容量
	private static int SHOPPINGQUEUESIZE = 4;
	
	public static ThreadPoolExecutor getInstance(){
		if(null == te){
			synchronized(ThreadPool.class){
				if(null == te){
					te = new ThreadPoolExecutor(SHOPPINGCOREPOOLSIZE, SHOPPINGMAXIMUMPOOLSIZE, SHOPPINGKEEPALIVETIME, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(SHOPPINGQUEUESIZE));
				}
			}
		}
		return te;
	}
	
}


构造器源码 jdk 1.6.0_43
/**
     * Creates a new <tt>ThreadPoolExecutor</tt> with the given initial
     * parameters and default thread factory and rejected execution handler.
     * It may be more convenient to use one of the {@link Executors} factory
     * methods instead of this general purpose constructor.
     *
     * @param corePoolSize the number of threads to keep in the
     * pool, even if they are idle.
     * @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 keepAliveTime
     * argument.
     * @param workQueue the queue to use for holding tasks before they
     * are executed. This queue will hold only the <tt>Runnable</tt>
     * tasks submitted by the <tt>execute</tt> method.
     * @throws IllegalArgumentException if corePoolSize or
     * keepAliveTime less than zero, or if maximumPoolSize less than or
     * equal to zero, or if corePoolSize greater than maximumPoolSize.
     * @throws NullPointerException if <tt>workQueue</tt> is null
     */
public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

参数详解:
corePoolSize :the number of threads to keep in the pool, even if they are idle.
核心线程数:核心线程会一直存活,即使没有任务需要执行 ,即使他们是空闲的。
          当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理。
          当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务。

maximumPoolSize: the maximum number of threads to allow in the pool.
最大线程数:线程池中允许的最大线程数。
          当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常。

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.
翻译:当线程数量大于核心时,这是多余的空闲线程在终止之前等待新任务的最大时间。
线程池中超过corePoolSize数目的空闲线程最大存活时间;

unit:the time unit for the keepAliveTime argument.
第三个参数keepAliveTime的时间单位

workQueue:the queue to use for holding tasks before they are executed. This queue will hold only the <tt>Runnable</tt> tasks submitted by the <tt>execute</tt> method.

二、ThreadPoolExecutor执行顺序:
     线程池按以下行为执行任务
(1)当线程数小于核心线程数时,创建线程。
(2)当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。
(3)当线程数大于等于核心线程数,且任务队列已满
          a.若线程数小于最大线程数,创建线程
          b.若线程数等于最大线程数,抛出异常,拒绝任务
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics