ThreadPoolExecutor源码解析
来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/05/16 09:44
先来看下ThreadPoolExecutor在tomcat7中的应用
/** 创建请求处理线程池 */
public void createExecutor() {
internalExecutor = true;
/** 创建任务队列 */
TaskQueue taskqueue = new TaskQueue();
/** 创建任务线程工厂 */
TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
/** 创建线程池ThreadPoolExecutor,核心线程10个,最大请求处理线程200个,线程空闲时间60s */
executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
/** 往任务队列中加入ThreadPoolExecutor */
taskqueue.setParent( (ThreadPoolExecutor) executor);
}
/** 创建子容器启动关闭线程池 */
protected void initInternal() throws LifecycleException {
BlockingQueue<Runnable> startStopQueue = new LinkedBlockingQueue<Runnable>();
/** 创建一个子容器启动和停止线程池,核心线程1个,最大线程数1个,空闲时间10TimeUnit */
startStopExecutor = new ThreadPoolExecutor(
getStartStopThreadsInternal(),
getStartStopThreadsInternal(), 10, TimeUnit.SECONDS,
startStopQueue);
/**
* keepAliveTime
* 设置线程在终止前可以保持空闲的时间限制。如果池中的当前线程数多于核心线程数,在不处理任务的情况下等待这一时间段之后,多余的线程将被终止。
*/
/** 允许核心线程在空闲时间超过keepAliveTime时将会终止,但是设置这个方法,keepAliveTime必须大于0,否则核心线程执行完就终止,线程池就无存在意义 */
startStopExecutor.allowCoreThreadTimeOut(true);
super.initInternal();
}
public void execute(Runnable command) {
// 如果任务为null,则抛出异常
if (command == null)
throw new NullPointerException();
// 如果当前线程池大小大于等于核心池大小
if (poolSize >= corePoolSize || !addIfUnderCorePoolSize(command)) {if (runState == RUNNING && workQueue.offer(command)) {
if (runState != RUNNING || poolSize == 0)
ensureQueuedTaskHandled(command);
}
else if (!addIfUnderMaximumPoolSize(command))
reject(command); // is shutdown or saturated
}
}
private boolean addIfUnderCorePoolSize(Runnable firstTask) {
Thread t = null;
final ReentrantLock mainLock = this.mainLock;
mainLock.lock();
try {
if (poolSize < corePoolSize && runState == RUNNING)
// 添加一个工作线程
t = addThread(firstTask);
} finally {
mainLock.unlock();
}
if (t == null)
return false;
t.start(); // 启动工作线程
return true;
}
private Thread addThread(Runnable firstTask) {
Worker w = new Worker(firstTask);
Thread t = threadFactory.newThread(w);
if (t != null) {
w.thread = t;
// 往线程池中添加一个线程
workers.add(w);
// 当前线程池大小
int nt = ++poolSize;
if (nt > largestPoolSize)
// 线程池历史最大线程数
largestPoolSize = nt;
}
return t;
}
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码解析
- Java 1.6 ThreadPoolExecutor源码解析
- Java 1.7 ThreadPoolExecutor源码解析
- ThreadPoolExecutor解析-主要源码研究
- 线程池ThreadPoolExecutor源码解析
- 源码解析 ThreadPoolExecutor JAVA1.8
- ThreadPoolExecutor源码解析(基于Java1.8)
- Java线程池源码解析(ThreadPoolExecutor)
- Java线程池ThreadPoolExecutor源码解析
- java.util.concurrent解析——ThreadPoolExecutor源码解析
- ThreadPoolExecutor源码
- ThreadPoolExecutor源码
- DFS poj 2488
- 最大流 poj 3436
- CLR笔记目录
- poj 1860 最短路径变形
- BFS进阶 poj 1184
- ThreadPoolExecutor源码解析
- poj 1166 简单搜索
- hdu 3664
- usaco 1.1 greedy gift givers
- poj 1002 stl map
- Contest on codeforce A
- JAVA面试试题集,很不错哦
- codeforce 11 04 A
- 最大流模板 Ford-Fulkerson and Dinic