1. @Async源码探究

1.1. 上代码

@SpringBootApplication
@EnableAsync
public class SpringbootLearnApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootLearnApplication.class, args);
} }
@Service
public class CreatingThread08Service { @Async
public void call(CountDownLatch countDownLatch) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is running");
countDownLatch.countDown();
System.out.println(Thread.currentThread().getName() + " is over"); }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootLearnApplicationTests { @Autowired
private CreatingThread08Service creatingThread08Service; private int count = 4; private CountDownLatch countDownLatch = new CountDownLatch(count); @Test
public void contextLoads() { StopWatch stopwatch = new StopWatch("async test");
stopwatch.start();
for (int i = 0; i < count; i++) {
creatingThread08Service.call(countDownLatch);
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
stopwatch.stop();
System.out.println(stopwatch.prettyPrint());
} }

结果

task-2 is running
task-2 is over
task-4 is running
task-4 is over
task-1 is running
task-1 is over
task-3 is running
task-3 is over
StopWatch 'async test': running time (millis) = 1018
-----------------------------------------
ms % Task name
-----------------------------------------
01018 100%

1.2. 提问

1.2.1. 加了该注解的方法,如果同时被调用n次,难道会创建n个线程?

  • 通过debugger源码找到如下信息,为它默认线程池设置属性

  • 我们把运行数量加大到count=20

  • 结果

task-1 is running
task-1 is over
task-8 is running
task-8 is over
task-5 is running
task-5 is over
task-3 is running
task-3 is over
task-4 is running
task-4 is over
task-7 is running
task-7 is over
task-2 is running
task-2 is over
task-6 is running
task-6 is over
task-1 is running
task-1 is over
task-8 is running
task-3 is running
task-3 is over
task-5 is running
task-5 is over
task-8 is over
task-4 is running
task-6 is running
task-6 is over
task-7 is running
task-7 is over
task-2 is running
task-2 is over
task-4 is over
task-5 is running
task-1 is running
task-8 is running
task-8 is over
task-1 is over
task-3 is running
task-3 is over
task-5 is over
StopWatch 'async test': running time (millis) = 3021
-----------------------------------------
ms % Task name
-----------------------------------------
03021 100%
  • 可以看出,如截图一致,它的运行核心执行线程数为8,且队列数很大,所以几乎不会再创建新的线程数,我特意sleep了1秒,20个任务,足够该线程池运行3遍,所以最大延迟3秒多

1.3. 那么核心线程数这个值能够变吗?

  • 可以,通过application.properties中修改spring.task.execution.pool.core-size=20的值,比如我现在改成20,那么打印结果如下
task-1 is running
task-9 is running
task-12 is running
task-15 is running
task-17 is running
task-11 is running
task-11 is over
task-1 is over
task-10 is running
task-10 is over
task-8 is running
task-8 is over
task-6 is running
task-6 is over
task-4 is running
task-4 is over
task-7 is running
task-7 is over
task-5 is running
task-3 is running
task-3 is over
task-20 is running
task-20 is over
task-17 is over
task-19 is running
task-19 is over
task-2 is running
task-2 is over
task-18 is running
task-18 is over
task-15 is over
task-16 is running
task-16 is over
task-12 is over
task-14 is running
task-14 is over
task-13 is running
task-13 is over
task-9 is over
task-5 is over
StopWatch 'async test': running time (millis) = 1020
-----------------------------------------
ms % Task name
-----------------------------------------
01020 100%
  • 所有线程只执行一遍,最大延迟就1秒多了

1.4. 总结

  • 使用该注解,会创建默认核心线程为8的线程池,它的很多属性是可以通过配置设置的,如下
spring.task.execution.pool.core-size=10
spring.task.execution.thread-name-prefix=mytask-
spring.task.execution.pool.queue-capacity=10
spring.task.execution.pool.max-size=20
spring.task.execution.pool.keep-alive=60s
spring.task.execution.pool.allow-core-thread-timeout=true
  • 根据业务需求设置合理的属性值,就相当于spring给你创建了线程池了

@Async源码探究的更多相关文章

  1. spring-cloud-sleuth+zipkin源码探究

    1. spring-cloud-sleuth+zipkin源码探究 1.1. 前言   粗略看了下spring cloud sleuth core源码,发现内容真的有点多,它支持了很多类型的链路追踪, ...

  2. spring-boot-2.0.3之quartz集成,数据源问题,源码探究

    前言 开心一刻 着火了,他报警说:119吗,我家发生火灾了. 119问:在哪里? 他说:在我家. 119问:具体点. 他说:在我家的厨房里. 119问:我说你现在的位置. 他说:我趴在桌子底下. 11 ...

  3. 【转载】Spring @Async 源码解读。

    由于工作中经常需要使用到异步操作,一直在使用@Async, 今天抽空学习了一下它的执行原理,刚好看到一篇写的很棒的文章,这里转载过来做个记录,感谢原作者的无私奉献. 原文章链接地址:https://w ...

  4. Vue源码探究-全局API

    Vue源码探究-全局API 本篇代码位于vue/src/core/global-api/ Vue暴露了一些全局API来强化功能开发,API的使用示例官网上都有说明,无需多言.这里主要来看一下全局API ...

  5. Vue源码探究-事件系统

    Vue源码探究-事件系统 本篇代码位于vue/src/core/instance/events.js 紧跟着生命周期之后的就是继续初始化事件相关的属性和方法.整个事件系统的代码相对其他模块来说非常简短 ...

  6. Vue源码探究-状态初始化

    Vue源码探究-状态初始化 Vue源码探究-源码文件组织 Vue源码探究-虚拟DOM的渲染 本篇代码位于vue/src/core/instance/state.js 继续随着核心类的初始化展开探索其他 ...

  7. Vue源码探究-源码文件组织

    Vue源码探究-源码文件组织 源码探究基于最新开发分支,当前发布版本为v2.5.17-beta.0 Vue 2.0版本的大整改不仅在于使用功能上的优化和调整,整个代码库也发生了天翻地覆的重组.可见随着 ...

  8. SpringBoot读取配置文件源码探究

    1. SpringBoot读取配置文件源码探究 1.1. 概览 springboot的源码是再原来的Spring源码上又包了一层,看过spring源码都知道,当我们从入口debug进去的时候,原来的S ...

  9. Sharding-Jdbc源码探究-读写分离

    1. Sharding-Jdbc源码探究-读写分离 1.1. 主入口 找到源码入口 这一个类围绕了springboot配置属性的加载,加载了spring.shardingsphere.datasour ...

随机推荐

  1. jq中

    1.jquery位置信息 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  2. mybatis-config.xml 知识点

    typeAliases 标签:配置别名,主要用于 XML 文件中的 resultType 参数. mappers 标签:配置所有的 mapper. MyBatis 和 Spring Boot 配合使用 ...

  3. python批量创建txt文件,以demo.txt内的内容为文件名

    #批量创建txt文件import sys,osa=open("demo.txt")n=0aList=[]for line in a.readlines(): aList.appen ...

  4. 12-numpy笔记-莫烦基本操作2

    代码 import numpy as np A = np.arange(3,15) print('-1-') print(A) print('-2-') print(A[3]) A = np.aran ...

  5. GitHub操作(五)

    GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本库格式进行托管,故名 GitHub. 1. 打开浏览器,输入GitHub的网址https://github.co ...

  6. 洛谷 P1182 数列分段 Section II

    洛谷 P1182 数列分段 Section II 洛谷传送门 题目描述 对于给定的一个长度为N的正整数数列A-iA−i,现要将其分成M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. ...

  7. C++面向对象程序设计学习笔记(7)

    模板与异常处理 模板的概念 模板是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型作为参数. 模板分为函数模板和类模板,它们分别允许用户构造模板类和模板函数 函数模板与模板函数 函数模板实际 ...

  8. 不使用xftp上传/下载文件到linux

    yum install lrzsz    # 安装软件 window端上传到linux端: 1. window端先压缩需上传的文件 2. linux端运行命令rz 3. 在弹出的窗口选择压缩好的文件, ...

  9. docker--发布docker镜像

    前戏 前面我们自己做了个docker镜像,我们可以上传到docker hub,别人就可以下载使用了 发布到docker hub 我们前面使用docker search 查找的镜像都是从docker h ...

  10. MacBook安装WIN7开机黑屏的解决办法

    同事的一台Macbook,全盘用diskgenius格式化为MBR分区,支持BIOS启动,安装ghost版本的WIN7,开机黑屏,看了网上的帖子没有用,插入启动盘按option键选择从优盘启动,用di ...