java线程池ThreadPoolExecutor源码分析

我们先来看一下线程池的执行过程图:

 

 ThreadPoolExecutor用一个AtomicInteger字段保存了2个状态

  1. workerCount (有效线程数) (占用29位)
  2. runState (线程池运行状态) (占用高3位)

线程池状态:

  1. RUNNING (接收新任务和执行队列中的任务)
  2. SHUTDOWN (不接收新任务,但是会继续执行队列中的任务)
  3. STOP (不接收新任务,不执行队列中的任务,且中断正在运行的任务)
  4. TIDYING (当所有任务都执行完毕(包含阻塞队列里面任务),工作线程数为0,并执行terminated方法)
  5. TERMINATED (当terminated方法执行完毕)

各状态的转换图:

//标记线程数和状态的混合值
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//线程位数
private static final int COUNT_BITS = Integer.SIZE - 3;
//线程最大个数(低29位)00011111111111111111111111111111
private static final int COUNT_MASK = (1 << COUNT_BITS) - 1;

//(高3位):11100000000000000000000000000000
private static final int RUNNING    = -1 << COUNT_BITS;
//(高3位):00000000000000000000000000000000
private static final int SHUTDOWN   =  0 << COUNT_BITS;
//(高3位):00100000000000000000000000000000
private static final int STOP       =  1 << COUNT_BITS;
//(高3位):01000000000000000000000000000000
private static final int TIDYING    =  2 << COUNT_BITS;
//(高3位):01100000000000000000000000000000
private static final int TERMINATED =  3 << COUNT_BITS;

//获取线程池运行状态
private static int runStateOf(int c)     { return c & ~COUNT_MASK; }
//获取线程个数
private static int workerCountOf(int c)  { return c & COUNT_MASK; }
//计算ctl新值
private static int ctlOf(int rs, int wc) { return rs | wc; }

//创建线程的工厂
private volatile ThreadFactory threadFactory;
//饱和策略,当队列满了并且线程个数达到maximunPoolSize后采取的策略
AbortPolicy(抛出异常)
CallerRunsPolicy(使用调用者所在线程来运行任务)
DiscardOldestPolicy(调用poll丢弃一个任务,执行当前任务)
DiscardPolicy(默默丢弃,不抛出异常)
private volatile RejectedExecutionHandler handler;
//存活时间。如果当前线程池中的线程数量比核心数量要多,并且是闲置状态的话,这些闲置的线程能存活的最大时间
private volatile long keepAliveTime;
//如果为false(默认值)核心线程会保持存活即使它们是空闲的
//如果为true,那么在keepAliveTime时间内没有运行任务,则它们会停止
private volatile boolean allowCoreThreadTimeOut;
//线程池核心线程个数
private volatile int corePoolSize;
//线程池最大线程数量
private volatile int maximumPoolSize;
//用于保存等待执行的任务的阻塞队列。 
private final BlockingQueue<Runnable> workQueue;
   

//构造函数
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方法:

public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    int c = ctl.get(); //获取workerCount+runState
    //如果当前线程数小于核心线程数
    if (workerCountOf(c) < corePoolSize) { 
        if (addWorker(command, true)) //添加工作
            return;
        c = ctl.get(); 
    }
    //如果线程池处于RUNNING状态,则添加任务到阻塞队列
    if (isRunning(c) && workQueue.offer(command)) { //状态为运行中且任务队列入队成功
        int recheck = ctl.get();
        if (! isRunning(recheck) && remove(command)) //如果当前线程池状态不是RUNNING则从队列删除任务,并执行拒绝策略
            reject(command); //拒绝
        //否者如果当前线程池线程空,则添加一个线程
        else if (workerCountOf(recheck) == 0)
            addWorker(null, false); 
    }
    //放入队列也失败,新建非核心线程来执行
    else if (!addWorker(command, false))
        reject(command); //失败,拒绝
}

也就是说下面几种情况下会返回false:

  • 当前线程池状态为STOP,TIDYING,TERMINATED
  • 当前线程池状态为SHUTDOWN并且已经有了第一个任务
  • 当前线程池状态为SHUTDOWN并且任务队列为空
 private boolean addWorker(Runnable firstTask, boolean core) {
        retry:
        //外循环,主要为了检查线程池运行状态
        for (int c = ctl.get();;) {
            // Check if queue empty only if necessary.
            if (runStateAtLeast(c, SHUTDOWN) //如果是SHUTDOWN以上状态
                && (runStateAtLeast(c, STOP) //如果是STOP以上状态  或 firstTask不为空 或workQueue.isEmpty
                    || firstTask != null
                    || workQueue.isEmpty()))
                return false;
            for (;;) {
                if (workerCountOf(c) 
                    >= ((core ? corePoolSize : maximumPoolSize) & COUNT_MASK)) //如果当前线程数大于等于核心或最大线程数
                    return false;
                if (compareAndIncrementWorkerCount(c)) //workerCount+1,退出外循环
                    break retry;
                //cas失败
                c = ctl.get();  // Re-read ctl
                if (runStateAtLeast(c, SHUTDOWN)) //非RUNNING状态,即状态改变,回到外循环重新判断状态
                    continue retry;
                // else CAS failed due to workerCount change; retry inner loop
                // 否则CAS失败是因为workerCount改变了,继续内循环
            }
        }
        //cas成功
        boolean workerStarted = false;
        boolean workerAdded = false;
        Worker w = null;
        try {
            w = new Worker(firstTask); //创建Worker
            final Thread t = w.thread; 
            if (t != null) {
                //设置独占锁,让workers添加同步
                final ReentrantLock mainLock = this.mainLock;
                mainLock.lock();
                try {
                    //重新检查线程池状态,为了避免在获取锁前调用了shutdown()
                    int c = ctl.get();
                    // 1、如果线程正在运行中 2、如果线程池状态为SHUTDOWN且firstTask不为空 
                    if (isRunning(c) ||
                        (runStateLessThan(c, STOP) && firstTask == null)) { 
                        if (t.getState() != Thread.State.NEW) //状态判断
                            throw new IllegalThreadStateException();
                        workers.add(w);
                        workerAdded = true;
                        int s = workers.size();
                        if (s > largestPoolSize) //设置最后的线程池大小
                            largestPoolSize = s;
                    }
                } finally {
                    mainLock.unlock();
                }
                if (workerAdded) {
                    t.start();
                    workerStarted = true;
                }
            }
        } finally {
            if (! workerStarted)
                addWorkerFailed(w); //失败任务操作
        }
        return workerStarted;
    }
}

工作线程Worker的执行

class Worker extends AbstractQueuedSynchronizer implements Runnable    
{
	Worker(Runnable firstTask) {
	    setState(-1); // 防止被中断直到运行
	    this.firstTask = firstTask;
	    //创建新线程,当前worker作为runnable
	    this.thread = getThreadFactory().newThread(this); 
	}

	public void run() {
	     runWorker(this);
	}
}

具体执行:

//运行任务
final void runWorker(Worker w) {
    Thread wt = Thread.currentThread();
    Runnable task = w.firstTask;
    w.firstTask = null;
    w.unlock(); // 设置状态为0,允许中断
    boolean completedAbruptly = true;
    try {
    	//任务存在
        while (task != null || (task = getTask()) != null) {
            w.lock();
            // 1、如果线程池当前状态至少是stop,则设置中断标志;
            // 2、如果不是,确保线程没被中断过
            if ((runStateAtLeast(ctl.get(), STOP) ||
                 (Thread.interrupted() &&
                  runStateAtLeast(ctl.get(), STOP))) &&
                !wt.isInterrupted())
                wt.interrupt(); //中断当前线程
            try {
            	//执行前
                beforeExecute(wt, task);
                try {
                    task.run();
                    //执行后
                    afterExecute(task, null);
                } catch (Throwable ex) {
                	//执行后异常
                    afterExecute(task, ex);
                    throw ex;
                }
            } finally {
                task = null;
                w.completedTasks++;
                w.unlock();
            }
        }
        completedAbruptly = false;
    } finally {
    	//停止worker
        processWorkerExit(w, completedAbruptly);
    }
}

//获取任务
private Runnable getTask() {
    boolean timedOut = false; // Did the last poll() time out?
    for (;;) {
        int c = ctl.get();
        // 1、如果当前状态为STOP以上,减少工作线程个数
        // 2、如果当前状态为SHUTDOWN以上,且workQueue为空,减少工作线程个数
        if (runStateAtLeast(c, SHUTDOWN)
            && (runStateAtLeast(c, STOP) || workQueue.isEmpty())) {
            decrementWorkerCount();
            return null;
        }
        int wc = workerCountOf(c);
        // Are workers subject to culling?
        // 这里并没有区分是否核心线程,所以说核心线程只是一个概念而已,真实起作用的只是核心线程数
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
        //(如果当前线程数>最大线程数||(需要减少线程 &&poll超时)) 且 (当前线程数>1||队列为空)
        if ((wc > maximumPoolSize || (timed && timedOut))
            && (wc > 1 || workQueue.isEmpty())) {
        	//减少线程数
            if (compareAndDecrementWorkerCount(c))
                return null;
            continue;
        }
        try {
        	//超时或阻塞
            Runnable r = timed ?
                workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                workQueue.take();
            if (r != null)
                return r;
            //超时
            timedOut = true;
        } catch (InterruptedException retry) {
            timedOut = false;
        }
    }
}

private void processWorkerExit(Worker w, boolean completedAbruptly, boolean allowCoreThreadTimeOut) {
	//执行出异常,减少工作线程
    if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
        decrementWorkerCount();

    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
    	//统计完成数量,移出完成的任务
        completedTaskCount += w.completedTasks;
        workers.remove(w);
    } finally {
        mainLock.unlock();
    }
    
    //如果当前是shutdown状态并且工作队列为空,尝试设置线程池状态为TERMINATED
    //或者当前是stop状态当前线程池里面没有活动线程
    tryTerminate();

    int c = ctl.get();
    //如果当前线程个数小于核心个数,则增加
    if (runStateLessThan(c, STOP)) {  //STOP状态以下
    	//没异常
        if (!completedAbruptly) {
        	//最小线程数
            int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
            //队列不为空
            if (min == 0 && ! workQueue.isEmpty())
            	//保留一个线程
                min = 1;
            if (workerCountOf(c) >= min)
                return; // replacement not needed
        }
        //增加核心线程数
        addWorker(null, false);
    }
}

/**
 * 当有以下两种情况之一时,转化状态为TERMINATED
 * 1、状态为SHUTDOWN && 工作线程为0 && 队列为空
 * 2、状态为STOP && 工作线程为0
 * 如果工作线程不为0,中断空闲的线程
 */
final void tryTerminate() {
    for (;;) {
        int c = ctl.get();
        /** 三种情况不做操作
         * 1、正在运行
         * 2、处于TIDYING及以上状态
         * 3、处于STOP以下状态,且队列为空
         */
        if (isRunning(c) ||
            runStateAtLeast(c, TIDYING) ||
            (runStateLessThan(c, STOP) && ! workQueue.isEmpty()))
            return;
        //如果工作线程不为0 中断一个线程?
        if (workerCountOf(c) != 0) { // Eligible to terminate
            interruptIdleWorkers(ONLY_ONE);
            return;
        }
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
        	//转变状态为TIDYING
            if (ctl.compareAndSet(c, ctlOf(TIDYING, 0))) {
                try {
                    terminated();
                } finally {
                    ctl.set(ctlOf(TERMINATED, 0));
                    termination.signalAll();
                }
                return;
            }
        } finally {
            mainLock.unlock();
        }
        // else retry on failed CAS
    }
}
/** 
 * 在队列的任务会继续执行,不接收新的任务,如果已经shutdown了,再次调用不会有影响
 */
public void shutdown() {
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
    	//权限检查
        checkShutdownAccess();
        //设置当前线程池状态为SHUTDOWN,如果已经是SHUTDOWN则直接返回
        advanceRunState(SHUTDOWN);
        //中断空闲的线程
        interruptIdleWorkers();
        //钩子
        onShutdown(); // hook for ScheduledThreadPoolExecutor
    } finally {
        mainLock.unlock();
    }
    tryTerminate();
}

//停止一切正在执行的任务。停止等待任务的线程,返回待执行任务的列表
public List<Runnable> shutdownNow() {
    List<Runnable> tasks;
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        checkShutdownAccess();
        advanceRunState(STOP);
        interruptWorkers();
        tasks = drainQueue();
    } finally {
        mainLock.unlock();
    }
    tryTerminate();
    return tasks;
}

Executors.newFixedThreadPool 执行过程

.

Executors.newCachedThreadPool 执行过程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值