看完【线程池的使用】,线程池简单使用应该是没有问题,在这里简单看下线程池是怎么样创建的。
1)、Executors类中方法,线程池创建的入口
//可缓存线程池
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
//线程数量固定的线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
//单线程的线程池
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
//线程数量固定的线程池,定时或周期性执行任务
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
//单线程的线程池,定时或周期性执行任务
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1));
}
2)、ScheduledThreadPoolExecutor类中方法,构造器
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,
new DelayedWorkQueue());
}
SynchronousQueue:一个无缓冲等待队列,或者说是一个不存储元素的阻塞队列,直接将任务交给消费者,只有等队列中的元素被消费掉后才能添加新的元素。
LinkedBlockingQueue:一个无界缓存等待队列。线程并行数量达到corePoolSize的数量后,剩余的任务(线程)会在阻塞队列里等待。
DelayedWorkQueue:一个专用的延迟队列。不是一个纯粹的队列(先进先出的数据结构),而是一个优先级队列,会根据延迟时间来对线程进行排序。
3)、线程池创建的公共方法,由于是punlic方法,所以创建线程池时也可以直接调用这个方法
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
corePoolSize:线程池中的核心线程数,可以是空闲线程,简单理解为创建线程池时的初始线程数量即可;
maximumPoolSize:线程池中允许存在的最大线程数;
keepAliveTime:空闲线程被终止的最大保留时间。默认情况,当线程池中线程数大于核心线程数且存在空闲线程时,空闲线程存在时间达到keepAliveTime的值,会被终止,直到线程池中线程数不超过核心线程数;当调用了allowCoreThreadTimeOut方法(值为true),核心线程数的设置将无效,也就是说,当空闲线程存在时间达到keepAliveTime的值时,会被终止;
unit:参数keepAliveTime的时间单位,取值:NANOSECONDS(纳秒)、TimeUnit.MICROSECONDS(微秒)、MILLISECONDS(毫秒)、SECONDS(秒)、MINUTES(分钟)、HOURS(小时)、DAYS(天);
workQueue:一个用来存储等待任务的阻塞队列;
4)、创建线程池方法
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;
}
threadFactory:线程工厂,用来创建线程;
handler:拒绝策略,线程池和队列都满了的情况下,处理提交的任务的策略,默认直接抛出异常
handler的取值:
a)、AborttPolicy:直接抛出异常;
b)、CallerRunsPolicy:由调用线程执行任务;
c)、DiscardOldestPolicy:丢弃队列里最前面的任务,重新尝试执行任务;
d)、DiscardPolicy:直接丢弃任务,不抛出异常。
至于ThreadPoolExecutor类和ScheduledThreadPoolExecutor类中的方法这里就不详细介绍。
如果有写的不对的地方,请大家多多批评指正,非常感谢!