从源码的角度分析ThreadPoolExecutor实现原理

线程池(ThreadPoolExecutor)的几个状态

  • RUNNING:Accept new tasks and process queued tasks(可接受新的任务并且执行已经加入队列的任务)
  • SHUTDOWN:Don't accept new tasks, but process queued tasks(不再接受新的任务,但是可以继续把已经加入队列的任务执行完成)
  • STOP:Don't accept new tasks, don't process queued tasks,and interrupt in-progress tasks(不接收新任务,不执行已经加入到队列中的任务,中断当前正在执行的任务)
  • TIDYING:All tasks have terminated, workerCount is zero,the thread transitioning to state TIDYING will run the terminated() hook method(所有的任务都结束了,workerCount 变为0,线程状态变为TIDYING ,然后会执行terminated()方法)
  • TERMINATED:terminated() has completed(terminated()方法已经执行完成)
线程池的状态切换过程
  • RUNNING -> SHUTDOWN:On invocation of shutdown(), perhaps implicitly in finalize()
  • (RUNNING or SHUTDOWN) -> STOP:On invocation of shutdownNow()
  • SHUTDOWN -> TIDYING:When both queue and pool are empty
  • STOP -> TIDYING:When pool is empty
  • TIDYING -> TERMINATED:When the terminated() hook method has completed
线程池的构造函数参数
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue 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; }

  • corePoolSize:核心线程数,即当前线程池中维持运行状态的线程数量,可以为0,也可以大于0,如果大于0就代表当前线程允许多个线程并发执行,如果等于0就代表线程池中的线程任务运行结束后就不在有线程处于运行状态,如果等于1就代表当前线程池在同一时间只允许一个线程在运行,类似于:Executors.newSingleThreadExecutor()
  • maximumPoolSize:线程池中最大可接受的线程数量,或者说任务数量
  • keepAliveTime:如果当前线程中中接受的线程数量已经超过和核心线程数,次数空闲线程在等待接收新线程可最多维持keepAliveTime的单位时间,超过这个时间,空闲线程将会终止
  • workQueue:任务队列,是阻塞队列,BlockingQueue
  • handler:当新put进来的任务被reject之后就会转给handler去处理,默认实现为抛出RejectedExecutionException异常
线程池的execute方法
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); }

  • isRunning方法判断当前的线程池是否存在已经在运行的线程,workQueue是任务队列,负责存放execute进来的command任务对象
  • 这里有两个地方会被线程池reject,也就是会抛出RejectedExecutionException异常,一个是当线程池已经被shutdown掉了,另一个当workQueue已经满了,而且当前线程池中正在工作的线程数量已经超过maximumPoolSize限制的数量了,这时候也会拒绝接受任务了
  • 如果当前线程池正在运行,并且线程池中的活跃线程数量还小于核心线程数corePoolSize,此时会直接start一个线程作为核心线程,线程池中运行的核心线程都是并发的
  • 如果当前线程池中的核心线程数已经达到限制,这时候会尝试吧任务加到任务队列中,等核心线程去遍历取出任务队列中的任务来执行
  • 线程池之所以说能够合理的管理线程资源,不乱开线程的原因就是控制核心线程数的运行数量,在线程池中并发的核心线程是有一个最大上线的(corePoolSize),所有外部execute进来的任务在核心线程数达到上线之后都会放进workQueue,这样做的好处就是开启的线程数最多只有corePoolSize,而其他多余的线程任务将以对象的方式存放到workQueue等待执行,所以申请了线程创建的只有corePoolSize个线程,而其他的任务只是占用了对象内存而已,避免了每一个线程都去start,既占用了对象内存,又申请了线程资源,对于java来说运行一个线程需要去申请线程的运行内存的,在底层是用pthread_create去创建一个linux线程,代价相对来说比较大,所以由线程池管理线程的这种方式限制了线程start的个数,避免造成过于的并发线程占用了大量的内存资源而导致OOM的问题
下面继续分析线程池如何管理运行线程,其实就一句话,维护一个线程队列,然后对这个线程队列进行存取操作
private boolean addWorker(Runnable firstTask, boolean core) { retry: for (; ; ) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && ! (rs == SHUTDOWN && firstTask == null && ! workQueue.isEmpty())) return false; for (; ; ) { int wc = workerCountOf(c); if (wc >= CAPACITY || wc >= (core ? corePoolSize : maximumPoolSize)) return false; if (compareAndIncrementWorkerCount(c)) break retry; c = ctl.get(); // Re-read ctl if (runStateOf(c) != rs) continue retry; // else CAS failed due to workerCount change; retry inner loop } }boolean workerStarted = false; boolean workerAdded = false; Worker w = null; try { w = new Worker(firstTask); final Thread t = w.thread; if (t != null) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { // Recheck while holding lock. // Back out on ThreadFactory failure or if // shut down before lock acquired. int rs = runStateOf(ctl.get()); if (rs < SHUTDOWN || (rs == SHUTDOWN && firstTask == null)) { if (t.isAlive()) // precheck that t is startable throw new IllegalThreadStateException(); workers.add(w); int s = workers.size(); if (s > largestPoolSize) largestPoolSize = s; workerAdded = true; } } finally { mainLock.unlock(); } if (workerAdded) { t.start(); workerStarted = true; } } } finally { if (! workerStarted) addWorkerFailed(w); } return workerStarted; }

每一个运行起来的核心线程都会被封装成一个Worker对象然后保存到workers列表中,核心线程start之后会执行Worker中的runWorker方法:
final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // allow interrupts boolean completedAbruptly = true; try { while (task != null || (task = getTask()) != null) { w.lock(); // If pool is stopping, ensure thread is interrupted; // if not, ensure thread is not interrupted.This // requires a recheck in second case to deal with // shutdownNow race while clearing interrupt if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { beforeExecute(wt, task); Throwable thrown = null; try { task.run(); } catch (RuntimeException x) { thrown = x; throw x; } catch (Error x) { thrown = x; throw x; } catch (Throwable x) { thrown = x; throw new Error(x); } finally { afterExecute(task, thrown); } } finally { task = null; w.completedTasks++; w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } }

这里面是一个while循环,反复的去workQueue队列里面去取任务出来执行,在同一个核心线程中,只有等上一个任务执行完成之后才能继续去执行下一个任务,所以同一个核心线程中的任务执行并不是并发的,但是不同核心线程中的任务执行是并发的,继续分析getTask方法:
private Runnable getTask() { boolean timedOut = false; // Did the last poll() time out?for (; ; ) { int c = ctl.get(); int rs = runStateOf(c); // Check if queue empty only if necessary. if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) { decrementWorkerCount(); return null; }int wc = workerCountOf(c); // Are workers subject to culling? boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; 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; } } }

getTask方法就是去workQueue任务队列中去取任务,这个方法里面还会对当前线程池的状态做判断,如果线程池被shutdown并且任务队列已经空了,或者线程池被shutdownNow了,这时候都会返回null,要是线程池还是在运行状态,那么这时候就会去判断如果当前核心线程数已经达到上线,或者限制核心线程数不能无限等待(allowCoreThreadTimeOut = true),会从workQueue中以poll的方式取任务,并且等待keepAliveTime时间,如果超过这个时间还没取出任务,此时poll方法返回null,然后进入下一次循环,但是最后如果是核心线程数已经达到上线,workQueue已经为空,那么此时getTask也会返回null,然后线程池会结束所有的核心线程,不再维持任何核心线程;这边举一个例子,假设keepAliveTime为0,那么当线程池中执行完所有的任务后,核心线程逐个马上就会结束并且释放,这时候要是有新的任务进来需要再重新开一个核心线程去执行,如果keepAliveTime为60秒,那么在线程池中完成所有任务后,核心线程会再等待60秒后才会结束运行,在这60秒之内如果还有新的任务进来就直接取出来接着执行,所以关于核心线程的维持活跃状态的实现是通过阻塞队列去实现的
shutdown和shutdownNow方法 shutdown方法在调用完了之后,线程池会把workQueue中剩下的任务都执行完了之后才会完全结束,而shutdownNow执行完了之后,二者都不允许再接收新的任务,workQueue中剩下的任务将不会再执行,具体的区别实现在一下几个实现
  • getTask方法中:
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) { decrementWorkerCount(); return null; }

【从源码的角度分析ThreadPoolExecutor实现原理】shutdown之后线程池的状态是SHUTDOWN,shutdownNow后线程池的状态是STOP,所以shutdownNow之后getTask会直接返回null,而shutdown之后如果workQueue不为空getTask还会取任务
  • processWorkerExit方法:
private void processWorkerExit(Worker w, boolean completedAbruptly) { 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(); }tryTerminate(); int c = ctl.get(); if (runStateLessThan(c, 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); } }

if (runStateLessThan(c, STOP))这个条件判断就是说要是只调用了shutdown,那就是满足的,次数会继续执行addWorker(null, false)来再次出发workQueue的任务存取执行
  • shutDown方法
public void shutdown() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); advanceRunState(SHUTDOWN); interruptIdleWorkers(); onShutdown(); // hook for ScheduledThreadPoolExecutor } finally { mainLock.unlock(); } tryTerminate(); }

private void interruptIdleWorkers() { interruptIdleWorkers(false); }

private void interruptIdleWorkers(boolean onlyOne) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) { Thread t = w.thread; if (!t.isInterrupted() && w.tryLock()) { try { t.interrupt(); } catch (SecurityException ignore) { } finally { w.unlock(); } } if (onlyOne) break; } } finally { mainLock.unlock(); } }

在shutdown调用后会去workers遍历里面的Worker对象,上文已经提到了,workers里面维护的Worker对象是核心线程的包装对象,所以这里是去中断核心线程,但是只是中断空闲的核心线程,什么叫空闲?w.tryLock()可以获取到锁的核心线程就是空闲的,因为在Worker运行的时候会先lock获取锁,在执行完成之后才unlock,所以要是Worker对象还在执行任务,这时候就不中断
  • shutdownNow
public List shutdownNow() { List tasks; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); advanceRunState(STOP); interruptWorkers(); tasks = drainQueue(); } finally { mainLock.unlock(); } tryTerminate(); return tasks; }

private void interruptWorkers() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (Worker w : workers) w.interruptIfStarted(); } finally { mainLock.unlock(); } }

shutdownNow方法就是直接遍历workers中的Worker对象并且进行中断操作,不管Worker是否是空闲状态,这里的线程中断就是调用Thread的interrupt方法,而在Worker的runWorker会去判断该Thread的的中断状态,根据中断状态来判断是否接着执行任务
总结
  • 线程池的好处就是合理的管理线程,避免造成过多的并发线程占用了大量内存资源,从何导致OOM
  • 线程池所谓的并发执行并不一定是并发,因为同一个核心线程中执行的任务是按照顺序执行的,并不是并发的,只有不同的核心线程的任务执行才是并发的
  • 多线程的实现可以用队列加上一个核心线程的方式去实现,可以开启一个核心线程负责遍历队列中的任务,然后在核心线程中去run,注意。如果核心线程只有一个的话,那么就无法达到并发效果,类似newSingleThreadExecutor方法创建出来的ThreadPoolExecutor,这时候execute多次,一定要等上一次的任务执行完成之后才能执行下一次的任务
  • Java线程start起来之后会有两个部分的内存占用,一个是JVM部分的线程对象资源,而真正开线程的其实是用linux的pthread_create去开启了一个linux线程实现异步的,所以这里还占用了系统底层的线程资源,因此开启一个Java线程的代价比直接用native方法去开启一个linux线程要大,占用的内存资源要多,所以要谨慎的使用java多线程,避免造成线程泄漏,或者OOM
声明
  • 我的博客即将搬运同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=2h71iay2bd6o8

    推荐阅读