本系列作为多线程系列的补充
核心线程与普通线程区别,这个问题之前没有注意到过,最近面试被问,所以就单独写个文章分析下
其实这个也非常简单了
构造线程池的时候Executors.newxxx的方法,点进去,会发现内部是通过
/**
* Creates a new {@code ThreadPoolExecutor} 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, 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.
* @throws IllegalArgumentException if one of the following holds:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@code maximumPoolSize < corePoolSize}
* @throws NullPointerException if {@code workQueue} is null
*/
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
这个构造方法来做的
再看下super
/**
* 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:
* {@code corePoolSize < 0}
* {@code keepAliveTime < 0}
* {@code maximumPoolSize <= 0}
* {@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 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;
}
我们这里一个参数一个参数的来看
第一个参数:corePoolSize 这个就是我面试被问的关键点了,其实就是核心线程在idle的时候会被keep,而普通的线程不会。
第二个参数:maximumPoolSize,最大线程数。
第三个参数:
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.
翻译一下就是:如果池内线程数大于core线程数,切这个时候多余的线程是idle的,则等多久terminating
第四个参数:unit 是秒啊,还是分钟,小时
第五个参数 workqueue 这个不同的线程池类型有不同类型的queue,比如
newSingleThreadExecutor:LinkedBlockingQueue
newFixedThreadPool:LinkedBlockingQueue
newCachedThreadPool:SynchronousQueue
newScheduledThreadPool:DelayedWorkQueue
另外:对于 newWorkStealingPool 使用的是ForkJoinPool
ForkJoinPool运用了Fork/Join原理,使用“分而治之”的思想,将大任务分拆成小任务分配给多个线程执行,最后合并得到最终结果,加快运算。
第六个参数:threadFactor 就是Thread的工厂类,提供thread的
第七个参数:
handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached
当线程边界或者队列容量达到上限时的处理类。。
=====================================================================
总结一下这里面的要点
1.核心线程idle的时候不会被回收,非核心线程会被回收 (当然,可以用 allowCoreThreadTimeOut来配置)
2.关于核心线程数:
newCachedThreadPool 0个
newSingleThreadExecutor 1个
newFixedThreadPool 看参数
newScheduledThreadPool 看参数个数
3.不同的线程有不同的queue