133

通俗易懂,各常用线程池执行的-流程图

 6 years ago
source link: https://juejin.im/post/5a28b37c6fb9a044fc44a103
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

作者:林冠宏 / 指尖下的幽灵

掘金:https://juejin.cn/user/1785262612681997

博客:http://www.cnblogs.com/linguanh/

GitHub : https://github.com/af913337456/

有时候花了大把时间去看一些东西却看不懂,是很 “ 蓝瘦 ” 的,花时间也是投资。

本文适合:

  • 曾了解过线程池却一直模模糊糊的人
  • 了解得差不多却对某些点依然疑惑的
  • 完全没看过的,建议你先去看看其他基础文章
  • 看过,却忘得差不多了,建议你先去回顾下

本文能给你的阅读回报

  • 适合的读者,尽可能让你彻底明白常用的线程池的知识相关点
  • 不适合的读者,能有个不错的概念,神童另谈

废话少说,我们开始。下图,皆可自行保存,常常阅之。日久,根深蒂固

默认构造函数

public ThreadPoolExecutor(
    int corePoolSize,
    int maximumPoolSize,
    long keepAliveTime,
    TimeUnit unit,
    BlockingQueue<Runnable> workQueue,
    ThreadFactory threadFactory,
    RejectedExecutionHandler handler
) 
{
    ....
}
复制代码

绝对易懂的构造方法参数讲解

参数名 作用
corePoolSize 队列没满时,线程最大并发数
maximumPoolSizes 队列满后线程能够达到的最大并发数
keepAliveTime 空闲线程过多久被回收的时间限制
unit keepAliveTime 的时间单位
workQueue 阻塞的队列类型
RejectedExecutionHandler 超出 maximumPoolSizes + workQueue 时,任务会交给RejectedExecutionHandler来处理

corePoolSize,maximumPoolSize,workQueue之间关系。

  • 当线程池中线程数小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。

  • 当线程池中线程数达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行 。

  • 当workQueue已满,且maximumPoolSize > corePoolSize时,新提交任务会创建新线程执行任务。

  • 当workQueue已满,且提交任务数超过maximumPoolSize,任务由RejectedExecutionHandler处理。

  • 当线程池中线程数超过corePoolSize,且超过这部分的空闲时间达到keepAliveTime时,回收这些线程。

  • 当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize范围内的线程空闲时间达到keepAliveTime也将回收。

一般流程图

1602fee11fcf165d~tplv-t2oaga2asx-zoom-in-crop-mark:4536:0:0:0.image

newFixedThreadPool 流程图

public static ExecutorService newFixedThreadPool(int nThreads){
    return new ThreadPoolExecutor(
            nThreads,   // corePoolSize
            nThreads,   // maximumPoolSize == corePoolSize
            0L,         // 空闲时间限制是 0
            TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>() // 无界阻塞队列
        );
}
复制代码



1602ff914d170169~tplv-t2oaga2asx-zoom-in-crop-mark:4536:0:0:0.image

newCacheThreadPool 流程图

public static ExecutorService newCachedThreadPool(){
    return new ThreadPoolExecutor(
        0,                  // corePoolSoze == 0
        Integer.MAX_VALUE,  // maximumPoolSize 非常大
        60L,                // 空闲判定是60 秒
        TimeUnit.SECONDS,
        // 神奇的无存储空间阻塞队列,每个 put 必须要等待一个 take
        new SynchronousQueue<Runnable>()  
    );
}

复制代码



1603006f3e0871c6~tplv-t2oaga2asx-zoom-in-crop-mark:4536:0:0:0.image

newSingleThreadPool 流程图

public static ExecutorService newSingleThreadExecutor() {
        return 
            new FinalizableDelegatedExecutorService
                (
                    new ThreadPoolExecutor
                        (
                            1,
                            1,
                            0L,
                            TimeUnit.MILLISECONDS,
                            new LinkedBlockingQueue<Runnable>(),
                            threadFactory
                        )
                );
    }
复制代码

可以看到除了多了个 FinalizableDelegatedExecutorService 代理,其初始化和 newFiexdThreadPool 的 nThreads = 1 的时候是一样的。 区别就在于:

  • newSingleThreadExecutor返回的ExcutorService在析构函数finalize()处会调用shutdown()
  • 如果我们没有对它调用shutdown(),那么可以确保它在被回收时调用shutdown()来终止线程。

使用ThreadFactory,可以改变线程的名称、线程组、优先级、守护进程状态,一般采用默认。

流程图略,请参考 newFiexdThreadPool,这里不再累赘。

还有一个定时任务线程池ScheduledThreadPool

它用来处理延时或定时任务,不常用


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK