Java线程池ThreadPoolExecutor中7个参数的含义

Java线程池ThreadPoolExecutor中7个参数的含义,第1张


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<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.acc = System.getSecurityManager() == null ? null : AccessController.getContext(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }

corePoolSize:核心线程数

核心线程数就是线程池中至少存在的线程数量,核心线程会一直存活,即使没有任务要执行,处于空闲状态,也不会被回收

当正在执行任务的线程数 = corePoolSize,且任务队列已满时,线程池会创建新线程来处理新提交过来的任务 (注意:新创建的线程会先处理新提交过来的任务而不是任务队列中的任务)


maximumPoolSize:最大线程数量,即线程池中最多只能容纳的线程数量

当正在执行任务的线程数 = maximumPoolSize,且任务队列已满时,那么线程池已经达到极限,会根据饱和策略拒绝新任务


keepAliveTime:空闲线程存活的时间值

如果时间值设置为0,将导致多余的线程在执行完任务后立即终止


TimeUnit:时间单位

TimeUnit.DAYS (天) TimeUnit.HOURS (小时) TimeUnit.MINUTES (分钟) TimeUnit.SECONDS (秒) TimeUnit.MICROSECONDS (微秒)


BlockingQueue:任务队列( 阻塞队列 ),当核心线程数达到最大时,新任务会放在队列中排队等待执行。可以设置以下几个值:

new ArrayBlockingQueue(10):由数组结构实现的有界阻塞队列,元素遵顼FIFO原则进行排序

new LinkedBlockingQueue(10):由链表结构实现的有界阻塞队列,元素遵顼FIFO原则进行排序。吞吐量通常高于ArrayBlockingQueue,静态工厂方法Executors.newFixedThreadPool()使用了这种阻塞队列

new SynchronousQueue(true):一个不存储元素的阻塞队列。每个线程入队 *** 作都必须等到另一个线程出队,否则入队 *** 作就会一直处于阻塞状态。吞吐量通常高于LinkedBlockingQueue,静态工厂方法Executors.newCachedThreadPool()使用了这种阻塞队列

new PriorityBlockingQueue(10):支持优先级的无界阻塞队列,内部使用数组存储数据(线程),达到容量时,会自动进行扩容。默认情况下,放入的元素会按照优先级进行升序排序,或者在创建PriorityBlockingQueue对象时,在参数中指定一个Comparator比较器,但注意:他无法保证相同优先级元素的顺序。。该类使用了ReentrantLock和Condition来确保多线程环境下的同步问题(控制同一时间只能有一个线程能进行入队出队 *** 作

new DelayBlockingQueue(10):

new LinkedTransferQueue(10):

new LinkedBlockingDeque(10):


ThreadFactory:线程工厂

线程池中的线程通过指定的线程工厂来创建


RejectedExecutionHandler:拒绝策略(线程池提供了4种拒绝策略),当线程数已经达到线了最大线程数,并且任务队列也已经满了,就会拒绝新提交过来的任务。

ThreadPoolExecutor.AbortPolicy(): 丢弃任务,抛出运行时异常 (如果不设置,则默认使用这种拒绝策略) ThreadPoolExecutor.CallerRunsPolicy():执行任务 ThreadPoolExecutor.DiscardPolicy():忽视,什么都不会发生 ThreadPoolExecutor.DiscardOldestPolicy():从任务队列中剔除最先进入队列( 最后一个执行 )的任务

注意:当线程池调用shutdown()后,线程池不会立即关闭,而是等待任务队列中的任务执行完毕后,线程池才会关闭。但在线程池调用了shutdown()后( 此时如果线程池尚未关闭 ),如果又有新的任务提交过来,那么也会通过拒绝策略拒绝新提交的任务

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1295195.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-10
下一篇2022-06-10

发表评论

登录后才能评论

评论列表(0条)

    保存