温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ThreadPoolExecutor线程池的示例分析

发布时间:2021-11-17 12:00:42 来源:亿速云 阅读:138 作者:小新 栏目:大数据

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

线程池参数

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:<br>
 *         {@code corePoolSize < 0}<br>
 *         {@code keepAliveTime < 0}<br>
 *         {@code maximumPoolSize <= 0}<br>
 *         {@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<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;
}

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

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

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

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

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

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

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

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

线程池工作原理

当一个新任务被提交时:

  1. 当前活跃线程数<corePoolSize,则创建一个新线程执行新任务;

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

  3. 当前活跃线程数>corePoolSize,且队列(workQueue)已满,且当前活跃线程数<maximumPoolSize,则继续创建一个新线程执行新任务;

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

当任务执行完成后:

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

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

线程池队列

通过构造方法可以知道,workQueue参数是BlockingQueue<Runnable>类型的,而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线程池的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI