在面试过程中,还是发现部分人对线程池的参数以及执行过程,不是很了解,所以需要记录一下
一、线程池的相关概念
线程池管理器,负责创建线程、销毁线程
工作线程,也就是真正工作的线程
任务接口,统一接口,执行任务,即run方法
任务队列,当提交的任务过多时,就需要队列进行缓冲
二、java中的API
其中
ThreadPoolExecutor是一个标准的线程池实现类
ScheduledThreadPoolExecutor扩展了ThreadPoolExecutor,加入了一些定时任务的实现,需要注意下面的两个方法
scheduleAtFixedRate,不管任务,周期到了之后,就立即执行
scheduleWithFixedDelay,等待上一个任务执行结束后,进入周期,开始执行
一般情况下,推荐使用scheduleWithFixedDelay,因为可能无法控制 任务的执行周期,为防止任务进行堆积,故推荐此方法
在java中,常用的线程池有4个
1、FixedThreadPool,创建定长的线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
2、CachedThreadPool,创建缓冲的线程池【推荐使用,但必须设置最大线程数,不要使用Integer.MAX_VALUE】
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
3、SingleThreadExecutor,创建单线程线程池
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
4、ScheduledThreadPool,创建定时任务线程池
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
//可以看到定时任务线程池的实现本质,是延迟队列
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
从这4个方法中,可以看到核心还是ThreadPoolExecutor,该类的构造函数
public ThreadPoolExecutor(int corePoolSize, //核心线程数,不会被回收
int maximumPoolSize, //最大线程数
long keepAliveTime, //存活时间,到达时间,会回收非核心线程数
TimeUnit unit, //时间单位
BlockingQueue<Runnable> workQueue) { //任务队列
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
Executors.defaultThreadFactory(), defaultHandler);
}
那这几个参数是如何影响,线程池的执行过程的呢?从代码注释中,可以清晰的看到执行过程
/**
* 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);
}
结论:
1、核心线程数 2、队列 3、最大线程数
所以,最大的处理容量,就是队列数+最大线程数
所以根据执行顺序,我们可以得到如下结论
(1)如果队列为无限队列,则最大线程数没有意义
(2)如果队列为有界队列,当其容量满了之后,则会启用新的线程继续工作
(3)如果队列满了,最大线程数也满了,后续来的任务则直接会被拒绝掉,抛出异常【可以使用RejectedExecutionHandler做异常捕获】
三、关闭线程池
1、shutdown
追加的任务会被拒绝,已经提交的任务会被继续执行完,所有任务执行完毕后,关闭线程池
2、shutdownNow
追加的任务会被拒绝,已经提交的任务会被中止,而正在运行的任务,也会被直接中止掉,然后关闭线程池,所以任务可能会抛出interruptException异常
四、如何确定合适的线程数量
1、计算型任务,cpu数量的1-2倍
2、IO型任务,需要多一些线程,可根据IO堵塞的时长,控制线程数量的多少,例如tomcat中,默认的最大线程数为200
3、一般CPU的利用率在80%左右,是正常值,说明其充分利用,大于则异常,小于则说明空闲,可以多开线程