赞
踩
/** * ThreadPoolExecutor中有一个控制状态的属性叫ctl,它是一个AtomicInteger类型的变量。线程池状态就是通过AtomicInteger类型的成员变量ctl来获取的。 */ private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); /** * COUNT_BITS固定为32-3=29 */ private static final int COUNT_BITS = Integer.SIZE - 3; /** * COUNT_MASK是低29位全1的int变量,用来作掩码 * c & COUNT_MASK 用来获取ctl的低29位 表示 工作线程数 * c & ~COUNT_MASK 用来获取ctl的高3位 表示 线程池状态 */ private static final int COUNT_MASK = (1 << COUNT_BITS) - 1; /** * 线程池创建后处于RUNNING状态。 * ctl前三位为 111 * 由于第一位是氟化物2,所以ctl是负数 */ private static final int RUNNING = -1 << COUNT_BITS; /** * 调用shutdown()方法后处于SHUTDOWN状态,线程池不能接受新的任务,清除一些空闲worker,不会等待阻塞队列的任务完成。 * ctl前三位为 000 */ private static final int SHUTDOWN = 0 << COUNT_BITS; /** * 调用shutdownNow()方法后处于STOP状态,线程池不能接受新的任务,中断所有线程,阻塞队列中没有被执行的任务全部丢弃。此时,poolsize=0,阻塞队列的size也为0。 * ctl前三位为 001 */ private static final int STOP = 1 << COUNT_BITS; /** * 当所有的任务已终止,ctl记录的”任务数量”为0,线程池会变为TIDYING状态。接着会执行terminated()函数。 * ctl前三位为 010 */ private static final int TIDYING = 2 << COUNT_BITS; /** * 线程池处在TIDYING状态时,执行完terminated()方法之后,就会由 TIDYING -> TERMINATED, 线程池被设置为TERMINATED状态。 * ctl前三位为 011 */ private static final int TERMINATED = 3 << COUNT_BITS; /** * 获取线程池状态(取ctl高三位) */ private static int runStateOf(int c) { return c & ~COUNT_MASK; } /** * 获取线程池工作线程数(取ctl低29位) */ private static int workerCountOf(int c) { return c & COUNT_MASK; } /** * 把线程池状态和工作线程数组装成一个int * 使用了或运算 */ private static int ctlOf(int rs, int wc) { return rs | wc; } private static boolean runStateLessThan(int c, int s) { return c < s; } private static boolean runStateAtLeast(int c, int s) { return c >= s; } private static boolean isRunning(int c) { return c < SHUTDOWN; } /** * 增加一个工作线程数 * 使用CAS */ private boolean compareAndIncrementWorkerCount(int expect) { return ctl.compareAndSet(expect, expect + 1); } /** * 减少一个工作线程数 * 使用CAS */ private boolean compareAndDecrementWorkerCount(int expect) { return ctl.compareAndSet(expect, expect - 1); } /** * 减少一个工作线程数 * 不论之前是啥,直接减少 */ private void decrementWorkerCount() { ctl.addAndGet(-1); } /** * 阻塞队列,用于存放等待运行的线程 * BlockingQueue workQueue:阻塞队列,维护着等待执行的Runnable任务对象。 * 常用的几个阻塞队列: * LinkedBlockingQueue * 链式阻塞队列,底层数据结构是链表,默认大小是Integer.MAX_VALUE,也可以指定大小。 * ArrayBlockingQueue * 数组阻塞队列,底层数据结构是数组,需要指定队列的大小。 * SynchronousQueue * 同步队列,内部容量为0,每个put操作必须等待一个take操作,反之亦然。 * DelayQueue * 延迟队列,该队列中的元素只有当其指定的延迟时间到了,才能够从队列中获取到该元素 。 */ private final BlockingQueue<Runnable> workQueue; private final ReentrantLock mainLock = new ReentrantLock(); private final HashSet<Worker> workers = new HashSet<>(); private final Condition termination = mainLock.newCondition(); private int largestPoolSize; private long completedTaskCount; /** * 线程工厂 */ private volatile ThreadFactory threadFactory; /** * 拒绝策略 */ private volatile RejectedExecutionHandler handler; private volatile long keepAliveTime; private volatile boolean allowCoreThreadTimeOut; private volatile int corePoolSize; private volatile int maximumPoolSize; private static final RejectedExecutionHandler defaultHandler = new AbortPolicy(); private static final RuntimePermission shutdownPerm = new RuntimePermission("modifyThread");
* 在构造方法中,创建了一个线程,线程的任务就是自己。 * 故addWorker方法调用addWorker方法源码下半部分中的第4步t.start,会触发Worker类的run方法被JVM调用。 */ private final class Worker extends AbstractQueuedSynchronizer implements Runnable { private static final long serialVersionUID = 6138294804551838833L; /** * Thread this worker is running in. Null if factory fails. */ @SuppressWarnings("serial") // Unlikely to be serializable final Thread thread; /** * Initial task to run. Possibly null. */ @SuppressWarnings("serial") // Not statically typed as Serializable Runnable firstTask; /** * Per-thread task counter */ volatile long completedTasks; // TODO: switch to AbstractQueuedLongSynchronizer and move // completedTasks into the lock word. /** * Creates with given first task and thread from ThreadFactory. * * @param firstTask the first task (null if none) */ Worker(Runnable firstTask) { setState(-1); // inhibit interrupts until runWorker this.firstTask = firstTask; this.thread = getThreadFactory().newThread(this); } /** * Delegates main run loop to outer runWorker. */ public void run() { runWorker(this); } // Lock methods // // The value 0 represents the unlocked state. // The value 1 represents the locked state. protected boolean isHeldExclusively() { return getState() != 0; } protected boolean tryAcquire(int unused) { if (compareAndSetState(0, 1)) { setExclusiveOwnerThread(Thread.currentThread()); return true; } return false; } protected boolean tryRelease(int unused) { setExclusiveOwnerThread(null); setState(0); return true; } public void lock() { acquire(1); } public boolean tryLock() { return tryAcquire(1); } public void unlock() { release(1); } public boolean isLocked() { return isHeldExclusively(); } void interruptIfStarted() { Thread t; if (getState() >= 0 && (t = thread) != null && !t.isInterrupted()) { try { t.interrupt(); } catch (SecurityException ignore) { } } } }
/** * ThreadPoolExecutor在创建线程时,会将线程封装成工作线程worker,并放入工作线程组中,然后这个worker反复从阻塞队列中拿任务去执行。 * 当第二个参数core为true时,创建核心线程执行任务 */ private boolean addWorker(Runnable firstTask, boolean core) { retry: for (int c = ctl.get(); ; ) { // Check if queue empty only if necessary. if (runStateAtLeast(c, SHUTDOWN) && (runStateAtLeast(c, STOP) || firstTask != null || workQueue.isEmpty())) return false; for (; ; ) { if (workerCountOf(c) // 1.如果core是ture,证明需要创建的线程为核心线程,则先判断当前线程是否大于核心线程 // 如果core是false,证明需要创建的是非核心线程,则先判断当前线程数是否大于总线程数 // 如果已有数量已经足够多了,那么就返回false >= ((core ? corePoolSize : maximumPoolSize) & COUNT_MASK)) return false; if (compareAndIncrementWorkerCount(c)) break retry; c = ctl.get(); // Re-read ctl if (runStateAtLeast(c, SHUTDOWN)) continue retry; // else CAS failed due to workerCount change; retry inner loop } } //上半部分是为了判断线程数量是否超出阈值,超过了就返回false。我们继续看下半部分 //下半部分是把任务包装成worker并运行 boolean workerStarted = false; boolean workerAdded = false; Worker w = null; try { // 1.创建一个worker对象 w = new Worker(firstTask); // 2.实例化一个Thread对象 final Thread t = w.thread; if (t != null) { // 3.线程池全局锁 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 c = ctl.get(); 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) { // 4.启动这个线程 t.start(); workerStarted = true; } } } finally { if (!workerStarted) addWorkerFailed(w); } return workerStarted; } private void addWorkerFailed(Worker w) { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { if (w != null) workers.remove(w); decrementWorkerCount(); tryTerminate(); } finally { mainLock.unlock(); } } 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); } } /** * 只要getTask方法不返回null,此runWorker的线程就不会退出。 * 当然,核心线程池中创建的线程想要拿到阻塞队列中的任务,先要判断线程池的状态,如果STOP或者TERMINATED,返回null。 */ private Runnable getTask() { boolean timedOut = false; // Did the last poll() time out? for (; ; ) { int c = ctl.get(); // Check if queue empty only if necessary. if (runStateAtLeast(c, SHUTDOWN) && (runStateAtLeast(c, STOP) || workQueue.isEmpty())) { decrementWorkerCount(); return null; } int wc = workerCountOf(c); // Are workers subject to culling? // 1.allowCoreThreadTimeOut变量默认是false,核心线程即使空闲也不会被销毁 // 如果为true,核心线程在keepAliveTime内仍空闲则会被销毁。 boolean timed = allowCoreThreadTimeOut || wc > corePoolSize; // 2.如果运行线程数超过了最大线程数,但是缓存队列已经空了,这时递减worker数量。 // 如果有设置允许线程超时或者线程数量超过了核心线程数量, // 并且线程在规定时间内均未poll到任务且队列为空则递减worker数量 if ((wc > maximumPoolSize || (timed && timedOut)) && (wc > 1 || workQueue.isEmpty())) { if (compareAndDecrementWorkerCount(c)) return null; continue; } // 3.如果timed为true(想想哪些情况下timed为true),则会调用workQueue的poll方法获取任务. // 超时时间是keepAliveTime。如果超过keepAliveTime时长, // poll返回了null,上边提到的while循序就会退出,线程也就执行完了。 // 如果timed为false(allowCoreThreadTimeOut为false // 且wc > corePoolSize为false),则会调用workQueue的take方法阻塞在当前。 // 队列中有任务加入时,线程被唤醒,take方法返回任务,并执行。 try { //核心线程的会一直卡在workQueue.take方法,被阻塞并挂起,不会占用CPU资源,直到拿到Runnable 然后返回 // (当然如果allowCoreThreadTimeOut设置为true,那么核心线程就会去调用poll方法,因为poll可能会返回null,所以这时候核心线程满足超时条件也会被销毁)。 //非核心线程会workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) ,如果超时还没有拿到, // 下一次循环判断compareAndDecrementWorkerCount就会返回null,Worker对象的run()方法循环体的判断为null,任务结束,然后线程被系统回收 。 Runnable r = timed ? workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take(); if (r != null) return r; timedOut = true; } catch (InterruptedException retry) { timedOut = false; } } } final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; // 1.线程启动之后,通过unlock方法释放锁 w.unlock(); // allow interrupts boolean completedAbruptly = true; try { // 2.Worker执行firstTask或从workQueue中获取任务,如果getTask方法不返回null,循环不退出 //runWorker方法中,首先去执行创建这个worker时就有的任务,当执行完这个任务后,worker的生命周期并没有结束, //在while循环中,worker会不断地调用getTask方法从阻塞队列中获取任务然后调用task.run()执行任务,从而达到复用线程的目的。 while (task != null || (task = getTask()) != null) { // 2.1进行加锁操作,保证thread不被其他线程中断(除非线程池被中断) 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 // 2.2检查线程池状态,倘若线程池处于中断状态,当前线程将中断。 if ((runStateAtLeast(ctl.get(), STOP) || (Thread.interrupted() && runStateAtLeast(ctl.get(), STOP))) && !wt.isInterrupted()) wt.interrupt(); try { // 2.3执行beforeExecute beforeExecute(wt, task); try { // 2.4执行任务 task.run(); // 2.5执行afterExecute方法 afterExecute(task, null); } catch (Throwable ex) { afterExecute(task, ex); throw ex; } } finally { task = null; w.completedTasks++; // 2.6解锁操作 w.unlock(); } } completedAbruptly = false; } finally { processWorkerExit(w, completedAbruptly); } } // Public constructors and methods /** * 五个参数的构造器 * * @param corePoolSize 该线程池中核心线程数最大值 * @param maximumPoolSize 该线程池中线程总数最大值 。 * @param keepAliveTime 非核心线程闲置超时时长。 * @param unit keepAliveTime的单位。 * @param workQueue 阻塞队列,维护着等待执行的Runnable任务对象。 */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } /** * 六个参数的构造器(一) * 多出来的参数为 * * @param threadFactory 创建线程的工厂 ,用于批量创建线程,统一在创建线程时设置一些参数,如是否守护线程、线程的优先级等。如果不指定,会新建一个默认的线程工厂。 */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); } /** * 六个参数的构造器(二) * 多出来的参数为 * * @param handler 拒绝处理策略,线程数量大于最大线程数就会采用拒绝处理策略 */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); } /** * 七个参数的构造器 */ 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; } /** * 提交任务的核心方法 * 线程总数量 < corePoolSize,无论线程是否空闲,都会新建一个核心线程执行任务(让核心线程数量快速达到corePoolSize,在核心线程数量 < corePoolSize时)。注意,这一步需要获得全局锁。 * 线程总数量 >= corePoolSize时,新来的线程任务会进入任务队列中等待,然后空闲的核心线程会依次去缓存队列中取任务来执行(体现了线程复用)。 * 当缓存队列满了,说明这个时候任务已经多到爆棚,需要一些“临时工”来执行这些任务了。于是会创建非核心线程去执行这个任务。注意,这一步需要获得全局锁。 * 缓存队列满了, 且总线程数达到了maximumPoolSize,则会采取上面提到的拒绝策略进行处理。 */ public void execute(Runnable command) { if (command == null) throw new NullPointerException(); int c = ctl.get(); // 1.当前线程数小于corePoolSize,则调用addWorker创建核心线程执行任务 if (workerCountOf(c) < corePoolSize) { //addWorker的第二个参数为true,表示创建的是核心线程 if (addWorker(command, true)) //进行返回 return; c = ctl.get(); } // 能执行到这里,说明当前线程数>=核心线程数 // 2.如果不小于corePoolSize,则将任务添加到workQueue队列。 if (isRunning(c) && workQueue.offer(command)) { //就算是添加成功了,也要再次检查 int recheck = ctl.get(); // 2.1 如果isRunning返回false(状态检查),则remove这个任务,然后执行拒绝策略。 if (!isRunning(recheck) && remove(command)) reject(command); //如果isRunning返回true,则不会进行remove,跳转到下面的else if判断 else if (workerCountOf(recheck) == 0) //线程池处于running状态,但是没有线程,则创建线程 addWorker(null, false); } // 3.如果放入workQueue失败,则创建非核心线程执行任务, // 如果这时创建非核心线程失败(当前线程总数不小于maximumPoolSize时),就会执行拒绝策略。 else if (!addWorker(command, false)) reject(command); } /** * 由调用线程处理该任务。 */ public static class CallerRunsPolicy implements RejectedExecutionHandler { public CallerRunsPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { r.run(); } } } /** * 默认拒绝处理策略,丢弃任务并抛出RejectedExecutionException异常。 */ public static class AbortPolicy implements RejectedExecutionHandler { public AbortPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString()); } } /** * 丢弃新来的任务,但是不抛出异常。 */ public static class DiscardPolicy implements RejectedExecutionHandler { public DiscardPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { } } /** * 丢弃队列头部(最旧的)的任务,然后重新尝试执行程序(如果再次失败,重复此过程)。 */ public static class DiscardOldestPolicy implements RejectedExecutionHandler { public DiscardOldestPolicy() { } public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { if (!e.isShutdown()) { e.getQueue().poll(); e.execute(r); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。