java线程池原理面试题_【腾讯阿里最全面试题】Java 线程池的实现原理,ThreadPoolExecutor关键参数解释...

【腾讯阿里最全面试题】Java 线程池的实现原理,ThreadPoolExecutor关键参数解释

Thread Pool, concurrency concept.java.util.concurrent.ThreadPoolExecutor:The API documentation :ThreadPoolExecutor. https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.htmlis comprehensive and detailed but difficult to imagine the working flow since there's no diagram or illustration, only descriptive text. But another clue that enlightens me to understand about it is the producer/consumer pattern.

21daf102bd76d07707c618435513625f.png

Simply put, tasks are sent from multiple producers into a waiting queue, waiting to be handled by multiple consumers.

a846cbb8288bef8ca8ac5727fbe0c125.png

https://medium.com/@vipulgupta_19290/threadpool-or-executor-framework-7007844cac52

afa0e7c003c0f5a13e68ca3744ebffbf.gif

Initially, there are 2 available threads in the thread pool. The 2 first tasks go through the queue and get redirected to idle Thread 1 and 2 for execution.When task 3 is enqueued, as there’s no available thread, ThreadFactory inside the ThreadPoolExecutor creates a new Thread 3 to handle the task. The same thing happens to task 4 and 5 with the creation of Thread 4 and 5. The maximum thread pool size is now reached, no more thread can be created.Task 6, 7, 8 are enqueued and wait until there’s an available Thread worker to handle.When the queue is full, it rejects any incoming tasks, so task 9 and 10 get blocked and redirected to a fallback function.Notes that if Thread 3, 4 or 5 is idle longer than keepAliveInMinutes period, it will get disposed. The thread pool shrinks its size to 2 (coreSize).In summary, there are 8 tasks get executed and 2 get rejected.That’s it! I hope this article is informative and useful for you. Feel free have any question or concern on the comment section below.Thank you for your time!

Model-Relationship

218f9c42e64cad5abea575df3069178d.png

ThreadPoolExecutor.png

其中,Worker 的模型如下:

5aab8fbd6667addbfab3e2045c537d07.png

ThreadPoolExecutor 线程池的几个主要参数的作用

public ThreadPoolExecutor(int corePoolSize,

int maximumPoolSize,

long keepAliveTime,

TimeUnit unit,

BlockingQueue workQueue,

ThreadFactory threadFactory,

RejectedExecutionHandler handler)

1、corePoolSize: 规定线程池有几个线程(worker)在运行。2、maximumPoolSize: 当workQueue满了,不能添加任务的时候,这个参数才会生效。规定线程池最多只能有多少个线程(worker)在执行。3、keepAliveTime: 超出corePoolSize大小的那些线程的生存时间,这些线程如果长时间没有执行任务并且超过了keepAliveTime设定的时间,就会消亡。4、unit: 生存时间对于的单位5、workQueue: 存放任务的队列6、threadFactory: 创建线程的工厂7、handler: 当workQueue已经满了,并且线程池线程数已经达到8、maximumPoolSize,将执行拒绝策略。

任务提交后的流程分析

用户通过submit提交一个任务。线程池会执行如下流程:

判断当前运行的worker数量是否超过corePoolSize,如果不超过corePoolSize。就创建一个worker直接执行该任务。—— 线程池最开始是没有worker在运行的

如果正在运行的worker数量超过或者等于corePoolSize,那么就将该任务加入到workQueue队列中去。

如果workQueue队列满了,也就是offer方法返回false的话,就检查当前运行的worker数量是否小于maximumPoolSize,如果小于就创建一个worker直接执行该任务。

如果当前运行的worker数量是否大于等于maximumPoolSize,那么就执行RejectedExecutionHandler来拒绝这个任务的提交。

参考资料

https://medium.com/@truongminhtriet96/playing-with-hystrix-thread-pool-c7eebb5b0ddc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值