Java ThreadPool源码简单的解析

来源:互联网 发布:windows.old文件夹 编辑:程序博客网 时间:2024/06/05 19:23
由于工作的需要,在实际的工作中使用java 线程池。现在对java的线程池ThreadPool做简单的解析。大体的主要类图关系:![类图](http://img.blog.csdn.net/20160803203424920)

其中对外暴露的核心方法为execute,具体的代码如下
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
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);
}

上面代码的大致意思是,可以使用下图展示核心执行流程

队列中的任务是通过任务执行驱动的不是通过扫描执行的。

0 0