public class ExecutorBuilder extends Object implements Builder<ThreadPoolExecutor>
ThreadPoolExecutor
建造者
1. 如果池中任务数 < corePoolSize -》 放入立即执行 2. 如果池中任务数 > corePoolSize -》 放入队列等待 3. 队列满 -》 新建线程立即执行 4. 执行中的线程 > maxPoolSize -》 触发handler(RejectedExecutionHandler)异常
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_QUEUE_CAPACITY
默认的等待队列容量
|
Constructor and Description |
---|
ExecutorBuilder() |
Modifier and Type | Method and Description |
---|---|
ThreadPoolExecutor |
build()
构建ThreadPoolExecutor
|
ExecutorService |
buildFinalizable()
创建有回收关闭功能的ExecutorService
|
static ExecutorBuilder |
create()
创建ExecutorBuilder,开始构建
|
ExecutorBuilder |
setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
设置线程执行超时后是否回收线程
|
ExecutorBuilder |
setCorePoolSize(int corePoolSize)
设置初始池大小,默认0
|
ExecutorBuilder |
setHandler(RejectedExecutionHandler handler)
设置当线程阻塞(block)时的异常处理器,所谓线程阻塞即线程池和等待队列已满,无法处理线程时采取的策略
此处可以使用JDK预定义的几种策略,见
RejectPolicy 枚举 |
ExecutorBuilder |
setKeepAliveTime(long keepAliveTime)
设置线程存活时间,即当池中线程多于初始大小时,多出的线程保留的时长,单位纳秒
|
ExecutorBuilder |
setKeepAliveTime(long keepAliveTime,
TimeUnit unit)
设置线程存活时间,即当池中线程多于初始大小时,多出的线程保留的时长
|
ExecutorBuilder |
setMaxPoolSize(int maxPoolSize)
设置最大池大小(允许同时执行的最大线程数)
|
ExecutorBuilder |
setThreadFactory(ThreadFactory threadFactory)
设置线程工厂,用于自定义线程创建
|
ExecutorBuilder |
setWorkQueue(BlockingQueue<Runnable> workQueue)
设置队列,用于存在未执行的线程
可选队列有: 1. |
ExecutorBuilder |
useArrayBlockingQueue(int capacity)
使用
ArrayBlockingQueue 做为等待队列有界队列,相对无界队列有利于控制队列大小,队列满时,运行线程小于maxPoolSize时会创建新线程,否则触发异常策略 |
ExecutorBuilder |
useSynchronousQueue()
使用
SynchronousQueue 做为等待队列(非公平策略)它将任务直接提交给线程而不保持它们。 |
ExecutorBuilder |
useSynchronousQueue(boolean fair)
使用
SynchronousQueue 做为等待队列它将任务直接提交给线程而不保持它们。 |
public static final int DEFAULT_QUEUE_CAPACITY
public ExecutorBuilder setCorePoolSize(int corePoolSize)
corePoolSize
- 初始池大小public ExecutorBuilder setMaxPoolSize(int maxPoolSize)
maxPoolSize
- 最大池大小(允许同时执行的最大线程数)public ExecutorBuilder setKeepAliveTime(long keepAliveTime, TimeUnit unit)
keepAliveTime
- 线程存活时间unit
- 单位public ExecutorBuilder setKeepAliveTime(long keepAliveTime)
keepAliveTime
- 线程存活时间,单位纳秒public ExecutorBuilder setWorkQueue(BlockingQueue<Runnable> workQueue)
1.SynchronousQueue
它将任务直接提交给线程而不保持它们。当运行线程小于maxPoolSize时会创建新线程,否则触发异常策略 2.LinkedBlockingQueue
默认无界队列,当运行线程大于corePoolSize时始终放入此队列,此时maxPoolSize无效。 当构造LinkedBlockingQueue对象时传入参数,变为有界队列,队列满时,运行线程小于maxPoolSize时会创建新线程,否则触发异常策略 3.ArrayBlockingQueue
有界队列,相对无界队列有利于控制队列大小,队列满时,运行线程小于maxPoolSize时会创建新线程,否则触发异常策略
workQueue
- 队列public ExecutorBuilder useArrayBlockingQueue(int capacity)
ArrayBlockingQueue
做为等待队列capacity
- 队列容量public ExecutorBuilder useSynchronousQueue()
SynchronousQueue
做为等待队列(非公平策略)public ExecutorBuilder useSynchronousQueue(boolean fair)
SynchronousQueue
做为等待队列fair
- 是否使用公平访问策略public ExecutorBuilder setThreadFactory(ThreadFactory threadFactory)
threadFactory
- 线程工厂ThreadFactoryBuilder
public ExecutorBuilder setHandler(RejectedExecutionHandler handler)
此处可以使用JDK预定义的几种策略,见RejectPolicy
枚举
handler
- RejectedExecutionHandler
RejectPolicy
public ExecutorBuilder setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
allowCoreThreadTimeOut
- 线程执行超时后是否回收线程public static ExecutorBuilder create()
public ThreadPoolExecutor build()
build
in interface Builder<ThreadPoolExecutor>
public ExecutorService buildFinalizable()
Copyright © 2024. All rights reserved.