mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
update 线程池最佳实践-图片地址
This commit is contained in:
parent
d2d83c15c6
commit
cb351262c4
@ -26,7 +26,7 @@
|
||||
|
||||
注意:**下面三个任务可能做的是同一件事情,也可能是不一样的事情。**
|
||||
|
||||

|
||||

|
||||
|
||||
### 如何使用线程池?
|
||||
|
||||
@ -147,7 +147,7 @@ Finished all threads
|
||||
|
||||
除此之外,我们还可以利用 `ThreadPoolExecutor` 的相关 API做一个简陋的监控。从下图可以看出, `ThreadPoolExecutor`提供了获取线程池当前的线程数和活跃线程数、已经执行完成的任务数、正在排队中的任务数等等。
|
||||
|
||||

|
||||

|
||||
|
||||
下面是一个简单的 Demo。`printThreadPoolStatus()`会每隔一秒打印出线程池的线程数、活跃线程数、完成的任务数、以及队列中的任务数。
|
||||
|
||||
@ -178,7 +178,7 @@ Finished all threads
|
||||
|
||||
**我们再来看一个真实的事故案例!** (本案例来源自:[《线程池运用不当的一次线上事故》](https://club.perfma.com/article/646639) ,很精彩的一个案例)
|
||||
|
||||

|
||||

|
||||
|
||||
上面的代码可能会存在死锁的情况,为什么呢?画个图给大家捋一捋。
|
||||
|
||||
@ -289,7 +289,7 @@ CPU 密集型简单理解就是利用 CPU 计算能力的任务比如你在内
|
||||
|
||||
**如何支持参数动态配置?** 且看 `ThreadPoolExecutor` 提供的下面这些方法。
|
||||
|
||||

|
||||

|
||||
|
||||
格外需要注意的是`corePoolSize`, 程序运行期间的时候,我们调用 `setCorePoolSize() `这个方法的话,线程池会首先判断当前工作线程数是否大于`corePoolSize`,如果大于的话就会回收工作线程。
|
||||
|
||||
@ -297,7 +297,7 @@ CPU 密集型简单理解就是利用 CPU 计算能力的任务比如你在内
|
||||
|
||||
最终实现的可动态修改线程池参数效果如下。👏👏👏
|
||||
|
||||

|
||||

|
||||
|
||||
还没看够?推荐 why神的[《如何设置线程池参数?美团给出了一个让面试官虎躯一震的回答。》](https://mp.weixin.qq.com/s/9HLuPcoWmTqAeFKa1kj-_A)这篇文章,深度剖析,很不错哦!
|
||||
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 162 KiB |
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
Binary file not shown.
After Width: | Height: | Size: 399 KiB |
Binary file not shown.
After Width: | Height: | Size: 86 KiB |
Binary file not shown.
After Width: | Height: | Size: 74 KiB |
Loading…
x
Reference in New Issue
Block a user