“`” 线程池实现类 ThreadPoolExecutorExecutor 框架最核心的类。**

<h3>ThreadPoolExecutor 类分析</h3>

<code>ThreadPoolExecutor</code> 类中提供的四个构造方法。我们来看最长的那个,其余三个都是在这个构造方法的基础上产生(其他几个构造方法说白点都是给定某些默认参数的构造方法比如默认制定拒绝策略是什么),这里就不贴代码讲了,比较简单。

<pre><code> /** * 用给定的初始参数创建一个新的ThreadPoolExecutor。 */ public ThreadPoolExecutor(int corePoolSize,//线程池的核心线程数量 int maximumPoolSize,//线程池的最大线程数 long keepAliveTime,//当线程数大于核心线程数时,多余的空闲线程存活的最长时间 TimeUnit unit,//时间单位 BlockingQueue<runnable> workQueue,//任务队列,用来储存等待执行任务的队列 ThreadFactory threadFactory,//线程工厂,用来创建线程,一般默认即可 RejectedExecutionHandler handler//拒绝策略,当提交的任务过多而不能及时处理时,我们可以定制策略来处理任务 ) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }</runnable></code></pre>

<strong>下面这些对创建 非常重要,在后面使用线程池的过程中你一定会用到!所以,务必拿着小本本记清楚。</strong>

<strong><code>ThreadPoolExecutor</code> 3 个最重要的参数:</strong>

<ul><li><strong><code>corePoolSize</code> :</strong> 核心线程数线程数定义了最小可以同时运行的线程数量。</li><li><strong><code>maximumPoolSize</code> :</strong> 当队列中存放的任务达到队列容量的时候,当前可以同时运行的线程数量变为最大线程数。</li><li><strong><code>workQueue</code>:</strong> 当新任务来的时候会先判断当前运行的线程数量是否达到核心线程数,如果达到的话,信任就会被存放在队列中。</li></ul>

<code>ThreadPoolExecutor</code>其他常见参数:

<ol><li><strong><code>keepAliveTime</code></strong>:当线程池中的线程数量大于 <code>corePoolSize</code> 的时候,如果这时没有新的任务提交,核心线程外的线程不会立即销毁,而是会等待,直到等待的时间超过了 <code>keepAliveTime</code>才会被回收销毁;</li><li><strong><code>unit</code></strong> : <code>keepAliveTime</code> 参数的时间单位。</li><li><strong><code>threadFactory</code></strong> :executor 创建新线程的时候会用到。</li><li><strong><code>handler</code></strong> :饱和策略。关于饱和策略下面单独介绍一下。</li></ol>

下面这张图可以加深你对线程池中各个参数的相互关系的理解(图片来源:《Java性能调优实战》):

<img src=""https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/%E7%BA%BF%E7%A8%8B%E6%B1%A0%E5%90%84%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E5%85%B3%E7%B3%BB.jpg"" referrerpolicy=""no-referrer"" alt=""线程池各个参数的关系"" style=""width: 100%;"">

<strong><code>ThreadPoolExecutor</code> 饱和策略定义:</strong>

如果当前同时运行的线程数量达到最大线程数量并且队列也已经被放满了任时,<code>ThreadPoolTaskExecutor</code> 定义一些策略:

<ul><li><strong><code>ThreadPoolExecutor.AbortPolicy</code></strong>:抛出 <code>RejectedExecutionException</code>来拒绝新任务的处理。</li><li><strong><code>ThreadPoolExecutor.CallerRunsPolicy</code></strong>:调用执行自己的线程运行任务,也就是直接在调用<code>execute</code>方法的线程中运行(<code>run</code>)被拒绝的任务,如果执行程序已关闭,则会丢弃该任务。因此这种策略会降低对于新任务提交速度,影响程序的整体性能。另外,这个策略喜欢增加队列容量。如果您的应用程序可以承受此延迟并且你不能任务丢弃任何一个任务请求的话,你可以选择这个策略。</li><li><strong><code>ThreadPoolExecutor.DiscardPolicy</code>:</strong> 不处理新任务,直接丢弃掉。</li><li><strong><code>ThreadPoolExecutor.DiscardOldestPolicy</code>:</strong> 此策略将丢弃最早的未处理的任务请求。</li></ul>

举个例子:

<blockquote><p>Spring 通过 <code>ThreadPoolTaskExecutor</code> 或者我们直接通过 <code>ThreadPoolExecutor</code> 的构造函数创建线程池的时候,当我们不指定 <code>RejectedExecutionHandler</code> 饱和策略的话来配置线程池的时候默认使用的是 <code>ThreadPoolExecutor.AbortPolicy</code>。在默认情况下,<code>ThreadPoolExecutor</code> 将抛出 <code>RejectedExecutionException</code> 来拒绝新来的任务 ,这代表你将丢失对这个任务的处理。 对于可伸缩的应用程序,建议使用 <code>ThreadPoolExecutor.CallerRunsPolicy</code>。当最大池被填满时,此策略为我们提供可伸缩队列。(这个直接查看 <code>ThreadPoolExecutor</code> 的构造函数源码就可以看出,比较简单的原因,这里就不贴代码了。)</p></blockquote>

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.