这些学习笔记其实早就写好了,只不过都是文档的形式,后续慢慢会整理成博客上传。这篇主要从源码的角度,讲解四个基本的线程池。个人目前水平有限,有些地方可能无法深入或者有所错漏的地方,还望谅解。但是随着日后水平增长,我会补全的。好了进入主题。
一、什么是线程池?
学术的定义是,线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。
个人学习的时候,喜欢想象成一个工厂,工厂内部对外封闭,工人们只将需要生产的任务交给工厂,然后工厂内部自动会将这些任务分配给不同的机器,然后完成生产作业。这里我们可以将线程理解为一台台的机器设备。
二、为什么要使用线程池?
那么为什么需要线程池呢?还是刚刚那个例子,如果不是这个大工厂内执行的话,而是在一块空地上一无所有。我每有一个新任务,都需要提交任务的人自带一台机器设备,那也太奢侈了,而且不现实。另外一方面,假设有钱任性,能够满足一每个人带一台设备,那也没有这么大的地方可以放置这么多设备,即使放得下肯定也是都挨着,影响效率。
所以我们可以对应得出线程池的两个优点
1)当出现大量任务的时候,不需要重复创建和销毁线程,因为这样做带来的开销十分巨大。
2)可以控制同一时间内并发执行的线程数,防止竞争带来的额外开销。
三、工作流程
那么接下来看工作流程,继续举例。当一个新任务被提交给工厂时,首先看常驻设备(核心线程)是否已经达到建厂的规划了,没有就再买设备(创建核心线程)。如果已经满了,就提交到这些设备的缓存队列(工作队列)中。这些设备一旦处理完当前任务,就可以从缓存队列中取出任务继续生产。如果缓存队列都满了,说明这段时间提交的任务太多了,需要额外再买设备加班加点生产(可以想象一下双十一),直到这个工厂放不下设备了,就执行拒绝策略,这个之后再谈。
那么计算机里的线程池的工作流程是这样的。
四、源码解读(觉得复杂的可以跳过,直接看后半部分对于ThreadPoolExecutor)
说到源码,相信很多人就头大,因为看不懂。但如果能够看得懂的话,学习起来一方面十分有成就感,另一方面学的十分透彻。首先来看executor的框架,看图。Executor接口是线程池体系的最顶级接口。
其他的好像和今天的主题关系不是很大,因此先不管它,主要看ExecutorService这个子接口。这个接口中只有一个抽象方法execute() 大概就是在将来会执行所给的命令,既有可能在新线程中执行,也可能在线程池的线程执行,或者在调用者线程执行。
public interface Executor {
/**
* Executes the given command at some time in the future. The command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the {@code Executor} implementation.
*
* @param command the runnable task
* @throws RejectedExecutionException if this task cannot be
* accepted for execution
* @throws NullPointerException if command is null
*/
void execute(Runnable command);
}
接下来是ExecutorService接口,这个接口中主要是一些对任务的一些操作,比如比如sumbit,shutdown。
public interface ExecutorService extends Executor {
void shutdown();
List<Runnable> shutdownNow();
<T> Future<T> submit(Callable<T> task);
<T> Future<T> submit(Runnable task, T result);
Future<?> submit(Runnable task);
}
然后是一个实现类和一个子接口。
/**
* Provides default implementations of {@link ExecutorService}
* execution methods. This class implements the {@code submit},
* {@code invokeAny} and {@code invokeAll} methods using a
* {@link RunnableFuture} returned by {@code newTaskFor}, which defaults
* to the {@link FutureTask} class provided in this package. For example,
* the implementation of {@code submit(Runnable)} creates an
* associated {@code RunnableFuture} that is executed and
* returned. Subclasses may override the {@code newTaskFor} methods
* to return {@code RunnableFuture} implementations other than
* {@code FutureTask}.
*
* <p><b>Extension example</b>. Here is a sketch of a class
* that customizes {@link ThreadPoolExecutor} to use
* a {@code CustomTask} class instead of the default {@code FutureTask}:
* <pre> {@code
* public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
*
* static class CustomTask<V> implements RunnableFuture<V> {...}
*
* protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
* return new CustomTask<V>(c);
* }
* protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
* return new CustomTask<V>(r, v);
* }
* // ... add constructors, etc.
* }}</pre>
*
* @since 1.5
* @author Doug Lea
*/
public abstract class AbstractExecutorService implements ExecutorService {
abstractExecutorService大概可以看出来,就是实现了ExecutorService,对其中的一些方法提供了默认实现。
/**
* A wrapper class that exposes only the ExecutorService methods
* of an ExecutorService implementation.
*/
static class DelegatedExecutorService extends AbstractExecutorService {
可以看到 DelegatedExecutorService实际上就是一个包装类,就不用管它了。
/**
* An {@link ExecutorService} for running {@link ForkJoinTask}s.
* A {@code ForkJoinPool} provides the entry point for submissions
* from non-{@code ForkJoinTask} clients, as well as management and
* monitoring operations.
省略中间的
public class ForkJoinPool extends AbstractExecutorService {
这个ForkJoinPool呢,我查了一下好像是JDK7加入的新线程池,用来分而划之大任务的。由于时间关系,暂且不提,感兴趣的可以看这一下这篇https://blog.csdn.net/niyuelin1990/article/details/78658251
然后就是我们今天的重点ThreadPoolExecutor。其他的代码先不管了,等用到的时候再看。首先来看这个最重要的构造方法。
实际上后面所讲的四种的基本线程池,都是调用的这个构造方法。
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
以下有几个参数。
corePoolSize 核心线程数 就是前面所说的现有的设备
maxiumPoolSize 最大线程数 就是前面所说工厂里允许存放的最大设备数量
keepAliveTime 存活时间 指线程允许存货的时间
unit 时间单位 存活时间的单位
workQueue 工作队列 规定存储队列的性质
而它的子类ScheduledThreadPoolExecutor,顾名思义则是规定了这些任务在未来的某个时刻进行执行。
五 、四种基本线程池
分别是定长型,计划型,单一线程型和缓存型。这几种线程都是调用上面的ThreadPoolExecutor()方法。
先看定长型,顾名思义,就是固定长度的线程池。
不多说看源码,还记得上面所说的参数吗?
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
对应的我们可以看到,定长型的特点就是,核心线程数和最大线程数相等,存活时间为0,注意这里的0不是0秒的意思,而是正无穷,换句话来说也就是一直存活,再看最后有一个工作队列,LinkedBlockingQueue<Runnable>
/**
* Creates a {@code LinkedBlockingQueue} with a capacity of
* {@link Integer#MAX_VALUE}.
*/
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
意思也就是说创建一个长度为正无穷的队列,如果n<0抛异常,并且是链式存储,分别有头尾指针。这个也很好理解,因为核心线程数和最大线程数相等,所以多出来 就必须都得在队列中存储。
再看计划型,它能够规定一段时间后执行线程。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
可以看到核心线程数就是输入的值,最大线程数是正无穷,存储的队列时DelayedWorkQueue。再看这个队列,底层结构大概意思就是这个一个基于堆和优先级队列的数据结构。然后将最接近当前执行时间的线程,放在堆的最上面。所以每次最先执行的都是最快要执行的。只是有一个疑惑,希望有人可以回答一下。
可以看到,这个的最大线程数是正无穷,而它的队列初始长度是16,然后当持有锁的时候可以1.5倍扩容。不是很理解为什么这么设计。既然选择扩容,为什么不类似于定长型,核心线程数和最大线程数相等,而队列因为能够扩容,相等于无穷。
/**
* Specialized delay queue. To mesh with TPE declarations, this
* class must be declared as a BlockingQueue<Runnable> even though
* it can only hold RunnableScheduledFutures.
*/
static class DelayedWorkQueue extends AbstractQueue<Runnable>
implements BlockingQueue<Runnable> {
/*
* A DelayedWorkQueue is based on a heap-based data structure
* like those in DelayQueue and PriorityQueue, except that
* every ScheduledFutureTask also records its index into the
* heap array. This eliminates the need to find a task upon
* cancellation, greatly speeding up removal (down from O(n)
* to O(log n)), and reducing garbage retention that would
* otherwise occur by waiting for the element to rise to top
* before clearing. But because the queue may also hold
* RunnableScheduledFutures that are not ScheduledFutureTasks,
* we are not guaranteed to have such indices available, in
* which case we fall back to linear search. (We expect that
* most tasks will not be decorated, and that the faster cases
* will be much more common.)
*
* All heap operations must record index changes -- mainly
* within siftUp and siftDown. Upon removal, a task's
* heapIndex is set to -1. Note that ScheduledFutureTasks can
* appear at most once in the queue (this need not be true for
* other kinds of tasks or work queues), so are uniquely
* identified by heapIndex.
*/
private static final int INITIAL_CAPACITY = 16;
/**
* Resizes the heap array. Call only when holding lock.
*/
private void grow() {
int oldCapacity = queue.length;
int newCapacity = oldCapacity + (oldCapacity >> 1); // grow 50%
if (newCapacity < 0) // overflow
newCapacity = Integer.MAX_VALUE;
queue = Arrays.copyOf(queue, newCapacity);
}
单一线程型,顾名思义,只有一个线程的线程池。所以我们也可以猜到,和定长型基本类似,队列长度无穷,唯一区别是只有一个线程池。看一下源码,果然如此,核心线程数和最大线程数都为1,使用的队列都是linkedBlockingQueue<Runnable>()
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
缓存型。一开始看到这个名字,还是不懂它是干嘛的。直到看到了源码,大概猜到了。核心线程数为0,最大线程数为正无穷,存活时间为60秒。所以这个线程池的特点就是,当有新任务提交时,如果现有线程池不够,就会创建新的线程池。如果一个线程空闲长达一分钟,就会进行回收。所使用的队列SynchronousQueue的特点是,它是没有容量的。如果说之前的队列都是隧道的话,这个队列就是一道门。隧道可以有长度,里面可以存放一定数量的汽车。但是门不同,门没有容量,要么在门外,要么在门内。并且采用的消费者模式,每一个put必须有一个take。
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
六、有关参考:
1、Java并发——Executor框架详解(Executor框架结构与框架成员)
https://blog.csdn.net/tongdanping/article/details/79604637
2、