常用线程池

ThreadPoolExecutor解析

image.png
阿里不推荐使用:
内部的无界队列,造成oom
自定义线程池:
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(10));

源码分析

image.png
image.png

流程图:

image.png

线程池处理流程

image.png
image.png
image.png

  1. private static final int COUNT_BITS = Integer.SIZE - 3;
  2. private static final int CAPACITY = (1 << COUNT_BITS) - 1;
  3. // runState is stored in the high-order bits
  4. private static final int RUNNING = -1 << COUNT_BITS;
  5. private static final int SHUTDOWN = 0 << COUNT_BITS;
  6. private static final int STOP = 1 << COUNT_BITS;
  7. private static final int TIDYING = 2 << COUNT_BITS;
  8. private static final int TERMINATED = 3 << COUNT_BITS;

COUNT_BITS:int位数 = 32-3
实际上使用32位中的高3位表示

execute方法:

  1. int c = ctl.get();
  2. 1、当前的线程数是否小于corePoolSize,如果是,
  3. 使用入参任务通过addWord方法创建一个新的线程,
  4. 如果能完成新线程创建exexute方法结束,成功提交任务;
  5. if (workerCountOf(c) < corePoolSize) {
  6. if (addWorker(command, true))
  7. return;
  8. c = ctl.get();
  9. }
  10. 2、没有完成任务提交;状态为运行并且能成功加入任务到工作队列后,
  11. 再进行一次check,如果状态在任务加入队列后变为了非运行(有可能是在执行到这里线程池shutdown了)
  12. 非运行状态下当然是需要reject
  13. 然后再判断当前线程数是否为0(有可能这个时候线程数变为了0),如是,新增一个线程;
  14. if (isRunning(c) && workQueue.offer(command)) {
  15. int recheck = ctl.get();
  16. if (! isRunning(recheck) && remove(command))
  17. reject(command);
  18. else if (workerCountOf(recheck) == 0)//判断当前工作线程池数是否为0
  19. addWorker(null, false);
  20. //如果是创建一个null任务,任务在堵塞队列存在了就会从队列中取出 这样做的意义是
  21. //保证线程池在running状态必须有一个任务在执行
  22. }
  23. 3、如果不能加入任务到工作队列,将尝试使用任务新增一个线程,如果失败,
  24. 则是线程池已经shutdown或者线程池已经达到饱和状态,所以reject;
  25. 拒绝策略不仅仅是在饱和状态下使用,
  26. 在线程池进入到关闭阶段同样需要使用到;:
  27. else if (!addWorker(command, false))
  28. reject(command);

z总结:

  1. 如果当前线程数小于corePoolSize, 通过addWorker创建新线程,
    如果能完成新线程创建execute方法结束,成成功提交任务
  2. 如果上一步没有完成提交任务,状态为运行态并且假如任务队列,再check一次
    如果状态再任务假如队列后变为非运行(线程池挂了),就执行reject,
    判断当前线程数是否为0,如果没了,新增一个线程
  3. 如果不能加入道工作队列,就尝试使用任务新增线程,如果线程池已经shutdown或满了,
    也会reject,
    reject:饱和状态下,线程池关闭阶段也会用到

addWorker方法

  1. private boolean addWorker(Runnable firstTask, boolean core) {
  2. retry: goto写法 用于重试
  3. for (;;) {
  4. int c = ctl.get();
  5. int rs = runStateOf(c);
  6. // Check if queue empty only if necessary.
  7. if (rs >= SHUTDOWN &&
  8. ! (rs == SHUTDOWN &&
  9. firstTask == null &&
  10. ! workQueue.isEmpty()))
  11. 线程状态非运行并且非shutdown状态任务为空,队列非空就不能新增线程了
  12. return false;
  13. for (;;) {
  14. int wc = workerCountOf(c);
  15. if (wc >= CAPACITY ||
  16. wc >= (core ? corePoolSize : maximumPoolSize))
  17. 当前现场大于等于最大值
  18. 等于核心线程数 非核心大于等于线程池数 说明达到了阈值
  19. 最大线程数 就不新增线程
  20. return false;
  21. if (compareAndIncrementWorkerCount(c)) ctl+1 工作线程池数量+1 如果成功
  22. 就跳出死循环。
  23. cas操作 如果为true 新增成功 退出
  24. break retry;
  25. c = ctl.get(); // Re-read ctl
  26. if (runStateOf(c) != rs)
  27. continue retry; 进来的状态和此时的状态发生改变 重头开始 重试
  28. // else CAS failed due to workerCount change; retry inner loop
  29. }
  30. }
  31. 上面主要是对ctl工作现场+1
  32. boolean workerStarted = false;
  33. boolean workerAdded = false;
  34. Worker w = null;
  35. try {
  36. w = new Worker(firstTask); 内部类 封装了线程和任务 通过threadfactory创建线程
  37. final Thread t = w.thread; 毎一个worker就是一个线程数
  38. if (t != null) {
  39. final ReentrantLock mainLock = this.mainLock;
  40. mainLock.lock();
  41. try {
  42. // Recheck while holding lock.
  43. // Back out on ThreadFactory failure or if
  44. // shut down before lock acquired.
  45. 重新获取当前线程状态
  46. int rs = runStateOf(ctl.get());
  47. 小于shutdown就是running状态
  48. if (rs < SHUTDOWN ||
  49. (rs == SHUTDOWN && firstTask == null)) {
  50. SHUTDOWN firstTask 为空是从队列中处理任务 那就可以放到集合中
  51. 线程还没start 就是alive就直接异常
  52. if (t.isAlive()) // precheck that t is startable
  53. throw new IllegalThreadStateException();
  54. workers.add(w);
  55. int s = workers.size();
  56. if (s > largestPoolSize)
  57. largestPoolSize = s; 记录最大线程数
  58. workerAdded = true;
  59. }
  60. } finally {
  61. mainLock.unlock();
  62. }
  63. if (workerAdded) {
  64. t.start(); 启动线程
  65. workerStarted = true;
  66. }
  67. }
  68. } finally {
  69. if (! workerStarted)
  70. addWorkerFailed(w);//失败回退 从wokers移除w 线程数减1 尝试结束线程池
  71. }
  72. return workerStarted;
  73. }
  1. private final class Worker
  2. extends AbstractQueuedSynchronizer
  3. implements Runnable
  4. {
  5. /**
  6. * This class will never be serialized, but we provide a
  7. * serialVersionUID to suppress a javac warning.
  8. */
  9. private static final long serialVersionUID = 6138294804551838833L;
  10. /** Thread this worker is running in. Null if factory fails. */
  11. 正在运行woker线程
  12. final Thread thread;
  13. /** Initial task to run. Possibly null. */
  14. 传入的任务
  15. Runnable firstTask;
  16. /** Per-thread task counter */
  17. 完成的任务数 监控用
  18. volatile long completedTasks;
  19. /**
  20. * Creates with given first task and thread from ThreadFactory.
  21. * @param firstTask the first task (null if none)
  22. */
  23. Worker(Runnable firstTask) {
  24. 禁止线程中断
  25. setState(-1); // inhibit interrupts until runWorker
  26. this.firstTask = firstTask;
  27. this.thread = getThreadFactory().newThread(this);
  28. }
  29. /** Delegates main run loop to outer runWorker */
  30. public void run() {
  31. runWorker(this);
  32. }

runwoker方法:

  1. final void runWorker(Worker w) {
  2. Thread wt = Thread.currentThread();//获取当前线程
  3. Runnable task = w.firstTask;
  4. w.firstTask = null;
  5. w.unlock(); // allow interrupts 把state从-1改为0 意思是可以允许中断
  6. boolean completedAbruptly = true;
  7. try { task不为空 或者阻塞队列中拿到了任务
  8. while (task != null || (task = getTask()) != null) {
  9. w.lock();
  10. // If pool is stopping, ensure thread is interrupted;
  11. // if not, ensure thread is not interrupted. This
  12. // requires a recheck in second case to deal with
  13. // shutdownNow race while clearing interrupt
  14. 如果当前线程池状态等于stop 就中断
  15. //Thread.interrupted() 中断标志
  16. if ((runStateAtLeast(ctl.get(), STOP) ||
  17. (Thread.interrupted() &&
  18. runStateAtLeast(ctl.get(), STOP))) &&
  19. !wt.isInterrupted())
  20. wt.interrupt();
  21. try {
  22. beforeExecute(wt, task);
  23. Throwable thrown = null;
  24. try {
  25. task.run();
  26. } catch (RuntimeException x) {
  27. thrown = x; throw x;
  28. } catch (Error x) {
  29. thrown = x; throw x;
  30. } catch (Throwable x) {
  31. thrown = x; throw new Error(x);
  32. } finally {
  33. afterExecute(task, thrown);
  34. }
  35. } finally {
  36. task = null; 这设置为空 等下次循环就会从队列里面获取
  37. w.completedTasks++; 完成任务数+1
  38. w.unlock();
  39. }
  40. }
  41. completedAbruptly = false;
  42. } finally {
  43. processWorkerExit(w, completedAbruptly);
  44. }
  45. }

getTask方法:

  1. private Runnable getTask() {
  2. boolean timedOut = false; // Did the last poll() time out?
  3. for (;;) {
  4. int c = ctl.get();
  5. int rs = runStateOf(c);//获取线程池运行状态
  6. shuitdown或者weikong 那就工作现场-1 同事返回为null
  7. // Check if queue empty only if necessary.
  8. if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
  9. decrementWorkerCount();
  10. return null;
  11. }
  12. 重新获取工作线程数
  13. int wc = workerCountOf(c);
  14. timed是标志超时销毁
  15. allowCoreThreadTimeOut true 核心线程池也是可以销毁的
  16. // Are workers subject to culling?
  17. boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
  18. if ((wc > maximumPoolSize || (timed && timedOut))
  19. && (wc > 1 || workQueue.isEmpty())) {
  20. if (compareAndDecrementWorkerCount(c))
  21. return null;
  22. continue;
  23. }
  24. try {
  25. Runnable r = timed ?
  26. workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
  27. workQueue.take();
  28. if (r != null)
  29. return r;
  30. timedOut = true;
  31. } catch (InterruptedException retry) {
  32. timedOut = false;
  33. }
  34. }
  35. }

processWorkerExit方法:

  1. private void processWorkerExit(Worker w, boolean completedAbruptly) {
  2. if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
  3. decrementWorkerCount();
  4. final ReentrantLock mainLock = this.mainLock;
  5. mainLock.lock();
  6. try {
  7. completedTaskCount += w.completedTasks;
  8. workers.remove(w);
  9. } finally {
  10. mainLock.unlock();
  11. }
  12. tryTerminate();
  13. int c = ctl.get();
  14. if (runStateLessThan(c, STOP)) {
  15. if (!completedAbruptly) {
  16. int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
  17. if (min == 0 && ! workQueue.isEmpty())
  18. min = 1;
  19. if (workerCountOf(c) >= min)
  20. return; // replacement not needed
  21. }
  22. addWorker(null, false);
  23. }
  24. }

ThreadPoolExecutor内部有实现4个拒绝策略:

(1)、CallerRunsPolicy,由调用execute方法提交任务的线程来执行这个任务;
(2)、AbortPolicy,抛出异常RejectedExecutionException拒绝提交任务;
(3)、DiscardPolicy,直接抛弃任务,不做任何处理;
(4)、DiscardOldestPolicy,去除任务队列中的第一个任务(最旧的),重新提交;
实际上需要自定义处理

  1. public class MyRejectedExecutionHandler implements RejectedExecutionHandler {
  2. @Override
  3. public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
  4. }
  5. }

ScheduledThreadPoolExecutor