使用线程池可以对线程进行统一的分配、监控和调优,降低系统资源消耗,提升系统稳定性。
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);
}
线程池处理流程如下:
在以上的线程池原理中提到了饱和策略,所谓的饱和策略就是当队列和线程池都满了,说明线程池处于饱和状态,那么就需要执行一种策略来处理提交的任务。以下是java线程池框架提供的4中饱和策略:
除了以上4中策略,还可以实现RejectedExecutionHandler接口,来自定义饱和策略,如记录日志或者持久化存储不能处理的任务。
线程池工作队列就是用来存储等待执行任务的阻塞队列。可以选择一下的几种队列:
在使用线程池之前,首先需要了解创建一个线程池所必须要传入的几个参数:
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;
}
向线程池中提交任务,有两种方式:execute()和submit()
线程池主要是对线程进行统一的资源调控、分配和监控,当线程池中线程出现问题时,可以根据线程池中提供的一些方法参数进行迅速的定位,以下API是常用的用于监控线程池的方法和属性:
除了以上这些方法,还可以通过继承线程池来自定义线程池,重写线程池中的一些方法,如terminated()、afterExecute()、beforeExecute(),通过重写这几个方法,就可以实现在线程池关闭、任务执行后、任务执行前对线程进行监控。