承接Spring源码情操陶冶-自定义节点的解析。线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深入的分析

spring配置文件样例

配置简单线程池

<task:executor keep-alive="60" queue-capacity="20" pool-size="5" rejection-policy="DISCARD">
</task:executor>

以上属性是spring基于task命令空间提供的对外属性,一般是线程池的基础属性,我们可以剖析下相应的解析类具体了解下spring是如何整合线程池的

ExecutorBeanDefinitionParser-解析task-executor节点

  1. 我们可以直接看下解析的具体方法doParse(),代码如下
	@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String keepAliveSeconds = element.getAttribute("keep-alive");
if (StringUtils.hasText(keepAliveSeconds)) {
builder.addPropertyValue("keepAliveSeconds", keepAliveSeconds);
}
String queueCapacity = element.getAttribute("queue-capacity");
if (StringUtils.hasText(queueCapacity)) {
builder.addPropertyValue("queueCapacity", queueCapacity);
}
configureRejectionPolicy(element, builder);
String poolSize = element.getAttribute("pool-size");
if (StringUtils.hasText(poolSize)) {
builder.addPropertyValue("poolSize", poolSize);
}
}

很简单就是读取我们第一部分所罗列的属性值,并塞入至BeanDefinitionBuilder对象的属性集合中。借此

对任务的拒绝策略属性解析方法configureRejectionPolicy()我们也可以简单的看下

	private void configureRejectionPolicy(Element element, BeanDefinitionBuilder builder) {
String rejectionPolicy = element.getAttribute("rejection-policy");
if (!StringUtils.hasText(rejectionPolicy)) {
return;
}
String prefix = "java.util.concurrent.ThreadPoolExecutor.";
if (builder.getRawBeanDefinition().getBeanClassName().contains("backport")) {
prefix = "edu.emory.mathcs.backport." + prefix;
}
String policyClassName;
if (rejectionPolicy.equals("ABORT")) {
policyClassName = prefix + "AbortPolicy";
}
else if (rejectionPolicy.equals("CALLER_RUNS")) {
policyClassName = prefix + "CallerRunsPolicy";
}
else if (rejectionPolicy.equals("DISCARD")) {
policyClassName = prefix + "DiscardPolicy";
}
else if (rejectionPolicy.equals("DISCARD_OLDEST")) {
policyClassName = prefix + "DiscardOldestPolicy";
}
else {
policyClassName = rejectionPolicy;
}
builder.addPropertyValue("rejectedExecutionHandler", new RootBeanDefinition(policyClassName));
}

由此可看出拒绝策略采取的基本上是java.util.concurrent.ThreadPoolExecutor工具包下的静态内部类,同时也支持用户自定义的拒绝策略,只需要实现RejectedExecutionHandler接口即可。对拒绝策略此处作下小总结

| rejection-policy(Spring)|  jdk对应类   |   含义  |
| :-------- | --------:| :------: |
| ABORT | java.util.concurrent.ThreadPoolExecutor.AbortPolicy | 抛出拒绝的异常信息 |
| CALLER_RUNS | java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy | 直接执行对应的任务(线程池不关闭) |
| DISCARD | java.util.concurrent.ThreadPoolExecutor.DiscardPolicy | 直接丢弃任务 |
| DISCARD_OLDEST | java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy | 直接丢弃队列最老的任务(最靠头部),塞入此任务到队列尾部 |
  1. bean实体类

    那么我们肯定要知道是spring的哪个bean来实例化我们的线程池配置呢?答案就在getBeanClassName()方法
	@Override
protected String getBeanClassName(Element element) {
return "org.springframework.scheduling.config.TaskExecutorFactoryBean";
}

直接通过TaskExecutorFactoryBeanbean工厂来实例化线程池,很有意思,我们也别忘了其获取实例化对象其实是调用了其内部的getObject()方法。我们继续跟踪把

TaskExecutorFactoryBean-线程池bean工厂类

看继承结构,我们细心的发现其实现了InitializingBean接口,那我们直接关注afterPropertiesSet()方法

	@Override
public void afterPropertiesSet() throws Exception {
// 实例化的bean类型为ThreadPoolTaskExecutor
BeanWrapper bw = new BeanWrapperImpl(ThreadPoolTaskExecutor.class);
determinePoolSizeRange(bw);
// 基本属性保存
if (this.queueCapacity != null) {
bw.setPropertyValue("queueCapacity", this.queueCapacity);
}
if (this.keepAliveSeconds != null) {
bw.setPropertyValue("keepAliveSeconds", this.keepAliveSeconds);
}
if (this.rejectedExecutionHandler != null) {
bw.setPropertyValue("rejectedExecutionHandler", this.rejectedExecutionHandler);
}
if (this.beanName != null) {
bw.setPropertyValue("threadNamePrefix", this.beanName + "-");
}
// 实例化ThreadPoolTaskExecutor对象
this.target = (TaskExecutor) bw.getWrappedInstance();
// 并执行相应的afterPropertiesSet方法
if (this.target instanceof InitializingBean) {
((InitializingBean) this.target).afterPropertiesSet();
}
}
  1. 真正实例化的对象为org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor

  2. task-executor指定的pool-size支持-作为分隔符,比如2-4,表示线程池核心线程数为2个,最大线程数为4;如果没有分隔符,则最大线程数等同于核心线程数

  3. task-executor如果指定pool-size0-4queue-capacity为null,则会指定线程池的allowCoreThreadTimeout为true,表明支持核心线程超时释放,默认不支持

  4. ThreadPoolTaskExecutor.class也是InitializingBean的实现类,也会被调用afterPropertiesSet()方法

ThreadPoolTaskExecutor#afterPropertiesSet()-实例化

直接查看initializeExecutor()初始化jdk对应的线程池

	// 默认的rejectedExecutionHandler为AbortPolicy策略
@Override
protected ExecutorService initializeExecutor(
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
// 先创建阻塞的队列
BlockingQueue<Runnable> queue = createQueue(this.queueCapacity);
// 创建线程池
ThreadPoolExecutor executor = new ThreadPoolExecutor(
this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS,
queue, threadFactory, rejectedExecutionHandler);
// 设置是否允许核心线程超时被收回。默认不允许
if (this.allowCoreThreadTimeOut) {
executor.allowCoreThreadTimeOut(true);
} this.threadPoolExecutor = executor;
return executor;
}

我们按照上述的注释,按照两步走进行解析

  1. 阻塞队列的创建
	protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
if (queueCapacity > 0) {
return new LinkedBlockingQueue<Runnable>(queueCapacity);
}
else {
return new SynchronousQueue<Runnable>();
}
}

默认情况下是创建LinkedBlockingQueue链式队列,因为默认的queueCapacity大小为Integer.MAX_VALUE。而queueCapacity为0的情况下则采取SynchronousQueue同步队列,其约定塞入一个元素必须等待另外的线程消费其内部的一个元素,其内部最多指定一个元素用于被消费

  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:<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;
}

篇幅限于过长,具体就不解释了直接看注释就明白了

TaskExecutorFactoryBean#getObject()-获取实体类

public TaskExecutor getObject() {
return this.target;
}

由上述可知this.target对应的class类为ThreadPoolTaskExecutor,其内部已实例化了ThreadPoolExecutor线程池对象

小结

  1. Spring创建线程池本质上是通过ThreadPoolExecutor的构造方法来进行创建的。由此可知jdkconcurrent工具包很有参考价值

  2. Spring默认指定的线程池队列为LinkedBlockingQueue链式队列,默认支持无限的任务添加,用户也可以指定queue-capacity来指定队列接受的最多任务数;并默认采用AbortPolicy策略来拒绝多余的任务

  3. Spring指定的pool-size支持-分隔符,具体解释见上文

  4. Spring的task-executor实例化后,用户可通过下述方式调用获取

    java TaskExecutor executor = ((TaskExecutorFactoryBean)applicationContext.getBean(TaskExecutorFactoryBean)).getObject();

Spring源码情操陶冶#task:executor解析器的更多相关文章

  1. Spring源码情操陶冶#task:scheduled-tasks解析器

    承接前文Spring源码情操陶冶#task:executor解析器,在前文基础上解析我们常用的spring中的定时任务的节点配置.备注:此文建立在spring的4.2.3.RELEASE版本 附例 S ...

  2. Spring源码情操陶冶-tx:advice解析器

    承接Spring源码情操陶冶-自定义节点的解析.本节关于事务进行简单的解析 spring配置文件样例 简单的事务配置,对save/delete开头的方法加事务,get/find开头的设置为不加事务只读 ...

  3. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  4. Spring源码情操陶冶-自定义节点的解析

    本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...

  5. Spring源码情操陶冶-任务定时器ConcurrentTaskScheduler

    承接前文Spring源码情操陶冶#task:scheduled-tasks解析器,本文在前文的基础上讲解单核心线程线程池的工作原理 应用附例 承接前文的例子,如下 <!--define bean ...

  6. Spring源码情操陶冶-AOP之Advice通知类解析与使用

    阅读本文请先稍微浏览下上篇文章Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器,本文则对aop模式的通知类作简单的分析 入口 根据前文讲解,我们知道通知类的 ...

  7. Spring源码情操陶冶-ComponentScanBeanDefinitionParser文件扫描解析器

    承接前文Spring源码情操陶冶-自定义节点的解析,本文讲述spring通过context:component-scan节点干了什么事 ComponentScanBeanDefinitionParse ...

  8. Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器

    本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...

  9. Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器

    本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...

随机推荐

  1. CentOS7更换国内源

    前言 CentOS 有个很方便的软件安装工具yum,但是默认安装完CentOS,系统里使用的是国外的CentOS更新源,这就造成了我们使用默认更新源安装或者更新软件时速度很慢的问题,甚至更新失败. 为 ...

  2. lavarel5.2官方文档阅读——架构基础

    <目录> 1.请求的生命周期 2.应用的架构 3.服务提供者 4.服务容器 5.Facades外立面(从这节起,看中文版的:https://phphub.org/topics/1783) ...

  3. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

  4. Git 简单入门(二)

    分支管理 分支的作用 提交不完整的代码到主分支上会导致别人不能正常开发 如果等代码全部写完再提交,存在丢失每天进度的风险 详见:https://segmentfault.com/q/101000001 ...

  5. atx-agent minicap、minitouch源码分析

    项目描述: 因为公司需要,特别研究了一下openatx系列手机群控源码 源码地址: https://github.com/openatx 该项目主要以go语言来编写服务端.集成 OpenSTF中核心组 ...

  6. 再谈反向传播(Back Propagation)

    此前写过一篇<BP算法基本原理推导----<机器学习>笔记>,但是感觉满纸公式,而且没有讲到BP算法的精妙之处,所以找了一些资料,加上自己的理解,再来谈一下BP.如有什么疏漏或 ...

  7. 吴恩达机器学习笔记54-开发与评价一个异常检测系统及其与监督学习的对比(Developing and Evaluating an Anomaly Detection System and the Comparison to Supervised Learning)

    一.开发与评价一个异常检测系统 异常检测算法是一个非监督学习算法,意味着我们无法根据结果变量

  8. [Swift]LeetCode308. 二维区域和检索 - 可变 $ Range Sum Query 2D - Mutable

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. [Swift]LeetCode741. 摘樱桃 | Cherry Pickup

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  10. springboot 实战之一站式开发体验

    都说springboot是新形势的主流框架工具,然而我的工作中并没有真正用到springboot: 都说springboot里面并没有什么新技术,不过是组合了现有的组件而已,但是自己却说不出来: 都说 ...