资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

ThreadPoolExecutor线程池的示例分析

小编给大家分享一下ThreadPoolExecutor线程池的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

十多年的巴楚网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整巴楚建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“巴楚网站设计”,“巴楚网站推广”以来,每个客户项目都认真落实执行。

线程池参数

ThreadPoolExecutor内部有四个构造方法,下面只列出参数最多的一个(另外三个都是补充默认参数后调用的这个):

/**
 * Creates a new {@code ThreadPoolExecutor} with the given initial
 * parameters.
 *
 * @param corePoolSize the number of threads to keep in the pool, even
 *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
 * @param maximumPoolSize the maximum number of threads to allow in the
 *        pool
 * @param 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.
 * @param unit the time unit for the {@code keepAliveTime} argument
 * @param 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.
 * @param threadFactory the factory to use when the executor
 *        creates a new thread
 * @param handler the handler to use when execution is blocked
 *        because the thread bounds and queue capacities are reached
 * @throws IllegalArgumentException if one of the following holds:
 *         {@code corePoolSize < 0}
 *         {@code keepAliveTime < 0}
 *         {@code maximumPoolSize <= 0}
 *         {@code maximumPoolSize < corePoolSize}  * @throws NullPointerException if {@code workQueue}  *         or {@code threadFactory} or {@code handler} is null  */ public ThreadPoolExecutor(int corePoolSize,                           int maximumPoolSize,                           long keepAliveTime,                           TimeUnit unit,                           BlockingQueue 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; }

其实从构造方法的注释中,我们就能知道每个参数的意思:

  • corePoolSize:保留在线程池中的线程数量,即使它们是闲置的,除非allowCoreThreadTimeOut被设置。

  • maximumPoolSize:线程池允许的最大线程数。

  • keepAliveTime:当线程数大于corePoolSize时,那些额外的空闲线程在停止前等待新任务的最大时间。

  • unit:keepAliveTime参数的时间单位。

  • workQueue:在任务执行之前,用于保存任务的队列,队列只会保存那些使用execute方法提交的可执行的任务。

  • threadFactory:当线程池创建一个新线程时所用到的工厂。

  • handler:当任务的执行因为线程数量和队列容量到达边界而被阻止时,所调用的handler。

线程池工作原理

当一个新任务被提交时:

  1. 当前活跃线程数

  2. 当前活跃线程数>corePoolSize,且队列(workQueue)未满时,则将新任务放入队列中;

  3. 当前活跃线程数>corePoolSize,且队列(workQueue)已满,且当前活跃线程数

  4. 当前活跃线程数>corePoolSize,且队列(workQueue)已满,且当前活跃线程数=maximumPoolSize,则执行拒绝策略(handler);

当任务执行完成后:

  1. 超出corePoolSize的空闲线程,在等待新任务时,如果超出了keepAliveTime,则线程会被销毁;

  2. 如果allowCoreThreadTimeOut被设置为true,那么corePoolSize以内的空闲线程,如果超出了keepAliveTime,则同样会被销毁。

线程池队列

通过构造方法可以知道,workQueue参数是BlockingQueue类型的,而BlockingQueue是JUC包里的一个接口,其实现有:

ThreadPoolExecutor线程池的示例分析

其中,常用的队列如下:

  1. ArrayBlockingQueue:规定大小的BlockingQueue,其构造必须指定大小。其所含的对象是FIFO顺序排序的。

  2. LinkedBlockingQueue:大小不固定的BlockingQueue,若其构造时指定大小,生成的BlockingQueue有大小限制,不指定大小,其大小有Integer.MAX_VALUE来决定。其所含的对象是FIFO顺序排序的。

  3. PriorityBlockingQueue:类似于LinkedBlockingQueue,但是其所含对象的排序不是FIFO,而是依据对象的自然顺序或者构造函数的Comparator决定。

  4. SynchronizedQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成。

线程池拒绝策略

同样,通过构造方法可以知道,handler参数是RejectedExecutionHandler类型的,而RejectedExecutionHandler同样是JUC包里的一个接口,其实现有:

ThreadPoolExecutor线程池的示例分析

  1. DiscardOldestPolicy:丢掉缓存在队列中的最早的任务,然后重新尝试运行新任务。

  2. AbortPolicy:直接拒绝添加新任务,并抛出异常。

  3. CallerRunsPolicy:使用当前线程执行该任务,而不是使用线程池中的线程。

  4. DiscardPolicy:删除提交的新任务 。

以上是“ThreadPoolExecutor线程池的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文题目:ThreadPoolExecutor线程池的示例分析
文章网址:http://cdkjz.cn/article/jhdhsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220