@@ -726,36 +726,36 @@ public class ThreadPoolExecutorDemo {
726
726
** Output:**
727
727
728
728
```
729
- pool-1-thread-2 Start. Time = Tue Nov 12 20:59:44 CST 2019
730
- pool-1-thread-5 Start. Time = Tue Nov 12 20:59:44 CST 2019
731
- pool-1-thread-4 Start. Time = Tue Nov 12 20:59:44 CST 2019
732
- pool-1-thread-1 Start. Time = Tue Nov 12 20:59:44 CST 2019
733
- pool-1-thread-3 Start. Time = Tue Nov 12 20:59:44 CST 2019
734
- pool-1-thread-5 End. Time = Tue Nov 12 20:59:49 CST 2019
735
- pool-1-thread-3 End. Time = Tue Nov 12 20:59:49 CST 2019
736
- pool-1-thread-2 End. Time = Tue Nov 12 20:59:49 CST 2019
737
- pool-1-thread-4 End. Time = Tue Nov 12 20:59:49 CST 2019
738
- pool-1-thread-1 End . Time = Tue Nov 12 20:59:49 CST 2019
739
- pool-1-thread-2 Start . Time = Tue Nov 12 20:59:49 CST 2019
740
- pool-1-thread-1 Start. Time = Tue Nov 12 20:59:49 CST 2019
741
- pool-1-thread-4 Start. Time = Tue Nov 12 20:59:49 CST 2019
742
- pool-1-thread-3 Start. Time = Tue Nov 12 20:59:49 CST 2019
743
- pool-1-thread-5 Start. Time = Tue Nov 12 20:59:49 CST 2019
744
- pool-1-thread-2 End. Time = Tue Nov 12 20:59:54 CST 2019
745
- pool-1-thread-3 End. Time = Tue Nov 12 20:59:54 CST 2019
746
- pool-1-thread-4 End. Time = Tue Nov 12 20:59:54 CST 2019
747
- pool-1-thread-5 End. Time = Tue Nov 12 20:59:54 CST 2019
748
- pool-1-thread-1 End. Time = Tue Nov 12 20:59:54 CST 2019
729
+ pool-1-thread-3 Start. Time = Sun Apr 12 11:14:37 CST 2020
730
+ pool-1-thread-5 Start. Time = Sun Apr 12 11:14:37 CST 2020
731
+ pool-1-thread-2 Start. Time = Sun Apr 12 11:14:37 CST 2020
732
+ pool-1-thread-1 Start. Time = Sun Apr 12 11:14:37 CST 2020
733
+ pool-1-thread-4 Start. Time = Sun Apr 12 11:14:37 CST 2020
734
+ pool-1-thread-3 End. Time = Sun Apr 12 11:14:42 CST 2020
735
+ pool-1-thread-4 End. Time = Sun Apr 12 11:14:42 CST 2020
736
+ pool-1-thread-1 End. Time = Sun Apr 12 11:14:42 CST 2020
737
+ pool-1-thread-5 End. Time = Sun Apr 12 11:14:42 CST 2020
738
+ pool-1-thread-1 Start . Time = Sun Apr 12 11:14:42 CST 2020
739
+ pool-1-thread-2 End . Time = Sun Apr 12 11:14:42 CST 2020
740
+ pool-1-thread-5 Start. Time = Sun Apr 12 11:14:42 CST 2020
741
+ pool-1-thread-4 Start. Time = Sun Apr 12 11:14:42 CST 2020
742
+ pool-1-thread-3 Start. Time = Sun Apr 12 11:14:42 CST 2020
743
+ pool-1-thread-2 Start. Time = Sun Apr 12 11:14:42 CST 2020
744
+ pool-1-thread-1 End. Time = Sun Apr 12 11:14:47 CST 2020
745
+ pool-1-thread-4 End. Time = Sun Apr 12 11:14:47 CST 2020
746
+ pool-1-thread-5 End. Time = Sun Apr 12 11:14:47 CST 2020
747
+ pool-1-thread-3 End. Time = Sun Apr 12 11:14:47 CST 2020
748
+ pool-1-thread-2 End. Time = Sun Apr 12 11:14:47 CST 2020
749
749
750
750
```
751
751
752
752
### 4.7 线程池原理分析
753
753
754
- 承接 4.6 节,我们通过代码输出结果可以看出:** 线程池每次会同时执行 5 个任务,这 5 个任务执行完之后,剩余的 5 个任务才会被执行 。** 大家可以先通过上面讲解的内容,分析一下到底是咋回事?(自己独立思考一会)
754
+ 承接 4.6 节,我们通过代码输出结果可以看出:** 线程池首先会先执行 5 个任务,然后这些任务有任务被执行完的话,就会去拿新的任务执行 。** 大家可以先通过上面讲解的内容,分析一下到底是咋回事?(自己独立思考一会)
755
755
756
756
现在,我们就分析上面的输出内容来简单分析一下线程池原理。
757
757
758
- ** 为了搞懂线程池的原理,我们需要首先分析一下 ` execute ` 方法。** 在 4.6 节中的 Demo 中我们使用 ` executor.execute(worker) ` 来提交一个任务到线程池中去,这个方法非常重要,下面我们来看看它的源码:
758
+ ** 为了搞懂线程池的原理,我们需要首先分析一下 ` execute ` 方法。** 在 4.6 节中的 Demo 中我们使用 ` executor.execute(worker) ` 来提交一个任务到线程池中去,这个方法非常重要,下面我们来看看它的源码:
759
759
760
760
``` java
761
761
// 存放线程池的运行状态 (runState) 和线程池内有效线程的数量 (workerCount)
@@ -802,13 +802,13 @@ public void execute(Runnable command) {
802
802
803
803
通过下图可以更好的对上面这 3 步做一个展示,下图是我为了省事直接从网上找到,原地址不明。
804
804
805
- ![ 图解线程池实现原理] ( https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7 /图解线程池实现原理.png)
805
+ ![ 图解线程池实现原理] ( images/java线程池学习总结 /图解线程池实现原理.png)
806
806
807
807
现在,让我们在回到 4.6 节我们写的 Demo, 现在应该是不是很容易就可以搞懂它的原理了呢?
808
808
809
809
没搞懂的话,也没关系,可以看看我的分析:
810
810
811
- > 我们在代码中模拟了 10 个任务,我们配置的核心线程数为 5 、等待队列容量为 100 ,所以每次只可能存在 5 个任务同时执行,剩下的 5 个任务会被放到等待队列中去。当前的 5 个任务执行完成后,才会执行剩下的 5 个任务 。
811
+ > 我们在代码中模拟了 10 个任务,我们配置的核心线程数为 5 、等待队列容量为 100 ,所以每次只可能存在 5 个任务同时执行,剩下的 5 个任务会被放到等待队列中去。当前的5个任务中如果有任务被执行完了,线程池就会去拿新的任务执行 。
812
812
813
813
## 5. Atomic 原子类
814
814
0 commit comments