|
| 1 | +package threadpool; |
| 2 | + |
| 3 | +/** |
| 4 | + * 线程池分析 |
| 5 | + * |
| 6 | + * @Author: zzStar |
| 7 | + * @Date: 10-12-2020 14:07 |
| 8 | + */ |
| 9 | +public class ThreadPoolAnalysis { |
| 10 | + |
| 11 | + /** |
| 12 | + * 线程池的组分部分: |
| 13 | + * 线程池管理器 |
| 14 | + * 工作线程 |
| 15 | + * 任务队列(blocking queue) |
| 16 | + * 任务接口(Task) |
| 17 | + */ |
| 18 | + |
| 19 | + /** |
| 20 | + * Executor <- ExecutorService <- AbstractExecutorService <- ThreadPoolExecutor |
| 21 | + */ |
| 22 | + |
| 23 | + /** |
| 24 | + * 线程池实现任务复用的原理 |
| 25 | + * 相同线程执行不同的任务,线程池是让固定的线程不断地从队列中取出任务并执行 |
| 26 | + */ |
| 27 | + |
| 28 | + /** |
| 29 | + * 线程池五种状态 |
| 30 | + * RUNNING |
| 31 | + * SHUTDOWN 不接受新任务,但处理排队任务 |
| 32 | + * STOP |
| 33 | + * TIDYING 所有任务都终止,workerCount为0时,线程会转到TIDYING状态,并将运行terminate()钩子方法 |
| 34 | + * TERMINATED terminate()运行完成 |
| 35 | + */ |
| 36 | + |
| 37 | + /** |
| 38 | + * 使用线程池注意点: |
| 39 | + * 避免任务堆积 |
| 40 | + * 避免线程数过度增加 |
| 41 | + * 排查线程泄露(线程回收不了,任务逻辑可能出问题) |
| 42 | + */ |
| 43 | +} |
| 44 | +/* |
| 45 | + public void execute(Runnable command) { |
| 46 | + if (command == null) |
| 47 | + throw new NullPointerException(); |
| 48 | + */ |
| 49 | +/* |
| 50 | + * Proceed in 3 steps: |
| 51 | + * |
| 52 | + * 1. If fewer than corePoolSize threads are running, try to |
| 53 | + * start a new thread with the given command as its first |
| 54 | + * task. The call to addWorker atomically checks runState and |
| 55 | + * workerCount, and so prevents false alarms that would add |
| 56 | + * threads when it shouldn't, by returning false. |
| 57 | + * |
| 58 | + * 2. If a task can be successfully queued, then we still need |
| 59 | + * to double-check whether we should have added a thread |
| 60 | + * (because existing ones died since last checking) or that |
| 61 | + * the pool shut down since entry into this method. So we |
| 62 | + * recheck state and if necessary roll back the enqueuing if |
| 63 | + * stopped, or start a new thread if there are none. |
| 64 | + * |
| 65 | + * 3. If we cannot queue task, then we try to add a new |
| 66 | + * thread. If it fails, we know we are shut down or saturated |
| 67 | + * and so reject the task. |
| 68 | + *//* |
| 69 | +
|
| 70 | + //ctl记录了线程状态和线程数 |
| 71 | + int c = ctl.get(); |
| 72 | + //少于则创建一个线程 |
| 73 | + if (workerCountOf(c) < corePoolSize) { |
| 74 | + //任务command |
| 75 | + if (addWorker(command, true)) |
| 76 | + return; |
| 77 | + c = ctl.get(); |
| 78 | + } |
| 79 | + // >=corePoolSize,先放入工作队列中 |
| 80 | + if (isRunning(c) && workQueue.offer(command)) { |
| 81 | + int recheck = ctl.get(); |
| 82 | + if (! isRunning(recheck) && remove(command)) |
| 83 | + reject(command); |
| 84 | + //防止任务提交却没有线程来执行 |
| 85 | + else if (workerCountOf(recheck) == 0) |
| 86 | + addWorker(null, false); |
| 87 | + } |
| 88 | + else if (!addWorker(command, false)) |
| 89 | + reject(command); |
| 90 | + } |
| 91 | +
|
| 92 | + final void runWorker(Worker w) { |
| 93 | + Thread wt = Thread.currentThread(); |
| 94 | + Runnable task = w.firstTask; |
| 95 | + w.firstTask = null; |
| 96 | + w.unlock(); // allow interrupts |
| 97 | + boolean completedAbruptly = true; |
| 98 | + try { |
| 99 | + //work不会停止 |
| 100 | + while (task != null || (task = getTask()) != null) { |
| 101 | + w.lock(); |
| 102 | + // If pool is stopping, ensure thread is interrupted; |
| 103 | + // if not, ensure thread is not interrupted. This |
| 104 | + // requires a recheck in second case to deal with |
| 105 | + // shutdownNow race while clearing interrupt |
| 106 | + if ((runStateAtLeast(ctl.get(), STOP) || |
| 107 | + (Thread.interrupted() && |
| 108 | + runStateAtLeast(ctl.get(), STOP))) && |
| 109 | + !wt.isInterrupted()) |
| 110 | + wt.interrupt(); |
| 111 | + try { |
| 112 | + beforeExecute(wt, task); |
| 113 | + try { |
| 114 | + task.run(); |
| 115 | + afterExecute(task, null); |
| 116 | + } catch (Throwable ex) { |
| 117 | + afterExecute(task, ex); |
| 118 | + throw ex; |
| 119 | + } |
| 120 | + } finally { |
| 121 | + task = null; |
| 122 | + w.completedTasks++; |
| 123 | + w.unlock(); |
| 124 | + } |
| 125 | + } |
| 126 | + completedAbruptly = false; |
| 127 | + } finally { |
| 128 | + processWorkerExit(w, completedAbruptly); |
| 129 | + } |
| 130 | + } |
| 131 | +*/ |
| 132 | + |
0 commit comments