线程池核心原理+动态调整线程池参数的实践,一次帮你搞定面试必问的线程池

线程池在内部实际上构建了一个生产者消费者模型,将线程和任务两者解耦,并不直接关联,从而良好的缓冲任务,复用线程。
摘要由CSDN通过智能技术生成

网上说的天花乱坠的,也不如直接看 Doug Lea 大佬源码的注释来的更加贴切些。

你敢信?掌握Java线程池原理,面试官会主动为你加薪,受宠若惊

corePoolSize:the number of threads to keep in the pool, even if they are idle, unless {@code allowCoreThreadTimeOut} is set

核心线程数:线程池中保留的线程数,即使它们是空闲的,除非设置 allowCoreThreadTimeOut。

maximumPoolSize:the maximum number of threads to allow in the pool

最大线程数:线程池中允许的最大线程数

keepAliveTime:when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

线程空闲时间:如果经过 keepAliveTime 时间后,超过核心线程数的线程还没有接受到新的任务,那就回收。

unit:the time unit for the {@code keepAliveTime} argument

单位:keepAliveTime 的时间单位

workQueue:the queue to use for holding tasks before they are executed. This queue will hold only the {@code Runnable} tasks submitted by the {@code execute} method.

存放待执行任务的队列:当提交的任务数超过核心线程数后,再提交的任务就存放在这里。它仅仅用来存放被 execute 方法提交的 Runnable 任务。

threadFactory:the factory to use when the executor creates a new thread

线程工厂:执行程序创建新线程时使用的工厂。比如我们项目中自定义的线程工厂,排查问题的时候,根据线程工厂的名称就知道这个线程来自哪里,很快地定位出问题,

handler :the handler to use when execution is blocked because the thread bounds and queue capacities are reached

拒绝策略:当队列里面放满了任务、最大线程数的线程都在工作时,这时继续提交的任务线程池就处理不了,应该执行怎么样的拒绝策略。

二、线程池的实现原理

==========

本文描述线程池是 JDK 8 中提供的 ThreadPoolExecutor 类,那我们就从 ThreadPoolExecutor 类来看下它的 UML 依赖关系。

2.1 总体设计

你敢信?掌握Java线程池原理,面试官会主动为你加薪,受宠若惊

  • 蓝色实线:继承关系

  • 绿色虚线:接口实现关系

  • 绿色实现:接口继承关系

<
  • 10
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值