Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 线程池 #305

Open
Bpazy opened this issue Oct 7, 2023 · 3 comments
Open

Java 线程池 #305

Bpazy opened this issue Oct 7, 2023 · 3 comments

Comments

@Bpazy
Copy link
Owner

Bpazy commented Oct 7, 2023

Refer: Java线程池实现原理及其在美团业务中的实践

https://github.com/dromara/dynamic-tp

@Bpazy
Copy link
Owner Author

Bpazy commented Oct 8, 2023

线程池解决的问题是什么

线程池解决的核心问题就是资源管理问题。在并发环境下,系统不能够确定在任意时刻中,有多少任务需要执行,有多少资源需要投入。这种不确定性将带来以下若干问题:

  1. 频繁申请/销毁资源和调度资源,将带来额外的消耗,可能会非常巨大。
  2. 对资源无限申请缺少抑制手段,易引发系统资源耗尽的风险。
  3. 系统无法合理管理内部的资源分布,会降低系统的稳定性。

为解决资源分配这个问题,线程池采用了“池化”(Pooling)思想。池化,顾名思义,是为了最大化收益并最小化风险,而将资源统一在一起管理的一种思想。

Pooling is the grouping together of resources (assets, equipment, personnel, effort, etc.) for the purposes of maximizing advantage or minimizing risk to the users. The term is used in finance, computing and equipment management.——wikipedia

“池化”思想不仅仅能应用在计算机领域,在金融、设备、人员管理、工作管理等领域也有相关的应用。

在计算机领域中的表现为:统一管理IT资源,包括服务器、存储、和网络资源等等。通过共享资源,使用户在低投入中获益。除去线程池,还有其他比较典型的几种使用策略包括:

  1. 内存池(Memory Pooling):预先申请内存,提升申请内存速度,减少内存碎片。
  2. 连接池(Connection Pooling):预先申请数据库连接,提升申请连接的速度,降低系统的开销。
  3. 实例池(Object Pooling):循环使用对象,减少资源在初始化和释放时的昂贵损耗。

@Bpazy
Copy link
Owner Author

Bpazy commented Oct 8, 2023

线程池核心参数设置

image

@Bpazy
Copy link
Owner Author

Bpazy commented Oct 18, 2023

线程安全、线程池复用(线程池配置,核心线程是否支持销毁,拒绝策略、优雅关闭、链路跟踪、ThreadLocal)、异步线程、加锁(资源竞争,尽量减少加锁)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant