0.敲开并发体系的大门
Tomcat 在启动EndPoint组件的时候,会创建一个线程池
public void createExecutor() {
internalExecutor = true;
//任务队列
TaskQueue taskqueue = new TaskQueue();
TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
taskqueue.setParent( (ThreadPoolExecutor) executor);
}
//此时的ThreadPoolExecutor不是Java里ThreadPoolExecutor,是Tomcat新建了一个同名的类继承了java里的线程池类
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
//new一下
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, new RejectHandler());
//启动所有核心线程
prestartAllCoreThreads();
}
ThreadPoolExecutor
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.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
TaskQueue
//TaskQueue是Tomcat自己实现的一个队列
//LinkedBlockingQueue 是java体系的 作者Doug Lea
public class TaskQueue extends LinkedBlockingQueue<Runnable> {
}
接下来的篇章会沿着这两条线索去分析
- 线程池原理
- 线程原理
- Linux任务调度
- Java锁机制
- Linux锁机制
- 并发容器处理逻辑
下一章从最简单的线程池开始