1、问题背景
JDK中的ThreadPoolExecutor线程池的构建参数比较多,构建过程复杂,需要按照自己的需求,定义构造参数,并按需要进行组装对象,所以针对ThreadPoolExecutor应用了建造者模式。
2、建造者模式
2.1 模式的定义与特点
建造者(Builder)模式的定义:指将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,这样的设计模式被称为建造者模式。它是将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。
该模式的主要优点如下:
- 封装性好,构建和表示分离。
- 扩展性好,各个具体的建造者相互独立,有利于系统的解耦。
- 客户端不必知道产品内部组成的细节,建造者可以对创建过程逐步细化,而不对其它模块产生任何影响,便于控制细节风险。
其缺点如下:
- 产品的组成部分必须相同,这限制了其使用范围。
- 如果产品的内部变化复杂,如果产品内部发生变化,则建造者也要同步修改,后期维护成本较大。
建造者(Builder)模式和工厂模式的关注点不同:建造者模式注重零部件的组装过程,而工厂方法模式更注重零部件的创建过程,但两者可以结合使用。
2.2 模式的结构与实现
建造者(Builder)模式由产品、抽象建造者、具体建造者、指挥者等 4 个要素构成,现在我们来分析其基本结构和实现方法。
1. 模式的结构
建造者(Builder)模式的主要角色如下。
- 产品角色(Product):它是包含多个组成部件的复杂对象,由具体建造者来创建其各个零部件。
- 抽象建造者(Builder):它是一个包含创建产品各个子部件的抽象方法的接口,通常还包含一个返回复杂产品的方法 getResult()。
- 具体建造者(Concrete Builder):实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。
- 指挥者(Director):它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息。
其结构图如图 所示。
3、核心源代码
这里只演示核心代码,省略无关代码。
/**
*
* 因为构建器参数过多,并且需要实现自定义,所以这里用建造者模式比较适合
*
*/
public static class Builder {
private String poolName;
private int corePoolSize;
private int maximumPoolSize;
private long keepAliveTime;
private TimeUnit timeUnit;
private BlockingQueue<Runnable> workQueue;
private ThreadFactory threadFactory;
private RejectedExecutionHandler rejectedExecutionHandler;
public Builder() {
}
public Builder poolName(String poolName) {
this.poolName = poolName;
return this;
}
public Builder corePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
return this;
}
public Builder maximumPoolSize(int maximumPoolSize) {
this.maximumPoolSize = maximumPoolSize;
return this;
}
public Builder keepAliveTime(long keepAliveTime) {
this.keepAliveTime = keepAliveTime;
return this;
}
public Builder timeUnit(TimeUnit timeUnit) {
this.timeUnit = timeUnit;
return this;
}
public Builder workQueue(BlockingQueue<Runnable> workQueue) {
this.workQueue = workQueue;
return this;
}
public Builder threadFactory(ThreadFactory threadFactory) {
this.threadFactory = threadFactory;
return this;
}
public Builder rejectedExecutionHandler(RejectedExecutionHandler rejectedExecutionHandler) {
this.rejectedExecutionHandler = rejectedExecutionHandler;
return this;
}
private void check() {
Assert.notNull(this.corePoolSize);
Assert.notNull(this.maximumPoolSize);
Assert.notNull(this.keepAliveTime);
Assert.notNull(this.timeUnit);
Assert.notNull(this.workQueue);
Assert.notNull(this.threadFactory);
Assert.notNull(this.rejectedExecutionHandler);
}
public ExecutorService build() {
this.check();
if (StringUtils.isEmpty(this.poolName)) {
this.poolName = ExecutorServiceUtil.genPoolName();
}
if (this.maximumPoolSize < this.corePoolSize) {
this.maximumPoolSize = this.corePoolSize;
}
final ExecutorService executorService = MonitoredThreadPoolExecutor.create(
this.poolName, this.corePoolSize, this.maximumPoolSize, this.keepAliveTime,
this.timeUnit, this.workQueue, this.threadFactory, this.rejectedExecutionHandler);
ExecutorServiceUtil.EXECUTOR_SERVICE_REGISTRY.put(this.poolName,
(MonitoredThreadPoolExecutor) executorService);
return executorService;
}
}
4、总结
这里恰当地使用了建造者模式,方便了线程池的创建工作,提高了可维护性。