简介
程序(代码)需要在计算机系统中运行,需要计算机系统的资源,资源是有限,不能你想要随时都有,正常的过程是:为程序(代码)创建一个线程 -> 等待cpu -> 执行 -> 结束 -> 销毁该线程,回收资源。如果大量程序(代码)执行的时候,计算机需要频繁的创建线程和销毁线程,但是程序是可以复用的,这次执行完,下次还是可以复用该线程,所以就出现的线程池。
线程池:程序先创建一定数量的空闲线程(这个过程其实也是一个线程),当有任务(程序)需要执行的时候,从中拿到一个空闲线程,执行完以后放回去。这样子计算机就可以不需要频繁的创建销毁线程。
线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务。线程池线程都是后台线程。每个线程都使用默认的堆栈大小,以默认的优先级运行,并处于多线程单元中。如果某个线程在托管代码中空闲(如正在等待某个事件),则线程池将插入另一个辅助线程来使所有处理器保持繁忙。如果所有线程池线程都始终保持繁忙,但队列中包含挂起的工作,则线程池将在一段时间后创建另一个辅助线程但线程的数目永远不会超过最大值。超过最大值的线程可以排队,但他们要等到其他线程完成后才启动。(来自百度百科)
java中的线程池
java1.5提供了四种线程池(后续版本又添加了几种)
Eexcutors.newFixedThreadPool
创建固定数量的线程,控制最大并发线程数。每次请求如果有空闲的线程,直接执行(等待cpu),否则将放入一个队列中。使用的队列类型是LinkedBlockingQueue(无边界)。
缺点:允许请求队列长度Integer.max,会堆积大量的请求,可能会导致OOM;
Exccutors.newSingleThreadPool()
创建一个线程,可以控制线程串行执行,并且用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。每次请求如果有空闲的线程,直接执行(等待cpu),否则将放入一个队列中。使用的队列类型是LinkedBlockingQueue(无边界)。
缺点:允许请求队列长度Integer.max,会堆积大量的请求,可能会导致OOM;
Executors.newCacheThreadPool()
创建Integer.max大小的线程,但是如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程。每次请求直接执行(等待cpu)。使用的队列类型是SynchronousQueue(直接提交队列)。
缺点:允许创建Integer.max线程,创建大量线程,可能导致OOM;
Executors.newScheduleThreadPool()
创建固定数量的线程,支持定时或者周期执行任务
ThreadPoolExecutor
从源码截图可以看到newFixedThreadPool、newCachedThreadPool都是通过一个ThreadPoolExecutor对象不同的参数来实现的。下面代码是ThreadPoolExecutor的源码,有七个参数。
/**
* 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:<br>
* {@code corePoolSize < 0}<br>
* {@code keepAliveTime < 0}<br>
* {@code maximumPoolSize <= 0}<br>
* {@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<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
corePoolSize:核心线程数,线程池一直保持的活动线程
maximumPoolSize:线程池里面允许最大线程数
keepAliveTime:当线程池的线程数超过核心线程数,线程空闲的时长超过这个值将被回收
unit:时间单位
workQueue:当线程池没有空闲线程,并且线程总数超过核心线程数,请求将放在队列中,请求队列类型:直接提交队列、有界队列、无界队列
threadFactory:线程工厂,用来生产线程
handler:拒绝策略,当请求队列满了,并且没有空闲的线程,请求将被拒绝。
workQueue-请求队列
当线程数超过核心线程(corePoolSize)的值,后续的请求将存放在队列中,常用的队列类型有三种:
- 直接提交队列SynchronousQueue;
- 有界队列ArrayBlockingQueue;
- 无界队列LinkedBlockingDeque(Integer.MAX_VALUE);
handler-拒绝策略
当请求堆满队列,并且线程数达到maximumPoolSize值,后续的请求将直接由handler处理,通常有四种:
- 直接丢弃(默认、报错,AbortPolicy)
- 丢弃(不报错,DiscardPolicy)
- 丢弃最老的(DiscardOldestPolicy)
- 调用主线程(CallerRunsPolicy)
注意什么时候线程数会超过corePoolSize值,activeNum有请求占用的线程数:
- activeNum < corePoolSize:直接运行
- maximumPoolSize > activeNum >= corePoolSize:讲请求加入queue队列,而不是添加新线程,直到请求无法加入队列,则创建新线程
- maximumPoolSize < activeNum :拒绝策略
/**
* Executes the given task sometime in the future. The task
* may execute in a new thread or in an existing pooled thread.
*
* If the task cannot be submitted for execution, either because this
* executor has been shutdown or because its capacity has been reached,
* the task is handled by the current {@code RejectedExecutionHandler}.
*
* @param command the task to execute
* @throws RejectedExecutionException at discretion of
* {@code RejectedExecutionHandler}, if the task
* cannot be accepted for execution
* @throws NullPointerException if {@code command} is null
*/
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
第一个IF判断的是活动线程是否大于核心线程数
第二个IF把请求加入队列中
第三个ELSE IF把请求加入队列失败,才创建新线程
FinalizableDelegatedExecutorService
newSingleThreadExecutor线程池实现多了FinalizableDelegatedExecutorService修饰。看源码发现多执行了shutdown(),就是手动进行垃圾回收。