Spring Boot默认提供了一个ThreadPoolTaskExecutor作为线程调度器,只需要在配置类中使用注解EnableAsync即可开启异步线程调度。在实际要执行的Bean中使用@Async注解来声明这个方法是异步方法,需要通过线程调度器来执行。

示例代码如下:

Application类,开启异步线程调度

@SpringBootApplication
@EnableAsync
public class TestApplication { public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}

异步任务类,声明execute()方法通过线程调度器执行

@Service
public class TestAsync { private static AtomicInteger count = new AtomicInteger(0); @Async
public String execute() {
System.out.println("begin execute TestAsync: " + count.incrementAndGet());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("finished execute TestAsync");
return "result";
}
}

要注意的问题:

(1)这里的@Async注解必须要声明在Bean对外提供的方法上,如果这个方法不是由其它类直接调用,而是这个类的其它方法间接调用,则不生效。
(2)@Async注解声明的方法,返回类型要么声明为void,要么声明为Future。因为方法是异步调用,因此无法立即返回结果,如果声明为其它返回类型,则获取到的是null。声明为Future,则可以获取到任务的执行结果。

Controller类

@RestController
public class TestAsyncController { @Autowired
private TestAsync testAsync; @RequestMapping(value = "/async", method = RequestMethod.GET)
public String async() {
System.out.println("before execute TestAsync");
String result = testAsync.outexecute();
System.out.println("after execute TestAsync");
return result;
}
}

这里获取到的result值null,因此获取这个返回值没有什么意义。

Spring Boot线程调度有以下几个参数可以配置(2.1版本之后才有):
spring.task.execution.pool.core-size # 核心线程数,默认为8
spring.task.execution.pool.queue-capacity # 队列容量,默认为无限大
spring.task.execution.pool.max-size # 最大线程数,默认为无限大
这三个参数的关系如下:
如果当前要执行的任务数超过core-size,则任务会放到队列里面等待执行,等核心线程中有任务执行完成之后,再取出队列中的任务进行调度执行。
如果等待队列已经满了,再收到新任务时,则核心线程会自动扩容,最大扩展到max-size。

spring.task.execution.pool.allow-core-thread-timeout # 是否允许回收空闲的线程,默认为true
spring.task.execution.pool.keep-alive # 空闲的线程可以保留多少秒,默认为60。如果超过这个时间没有任务调度,则线程会被回收
spring.task.execution.thread-name-prefix # 线程名前缀,默认为thread-

自定义线程调度器
如果不想使用Spring Boot自带的线程调度器,可以通过实现AsyncConfigurer接口来定义自己的线程调度器。
示例代码如下:

@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer { @Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("MyExecutor-");
executor.initialize();
return executor;
} @Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return MyAsyncUncaughtExceptionHandler();
}
}

  

SpringBoot的线程调度的更多相关文章

  1. SpringBoot系列九:SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 服务整合 2.背景 在进行项目开发的时候经常会遇见以下的几个问题:需要进行邮件发送.定时的任务调 ...

  2. 5分钟理解 SpringBoot 响应式的核心-Reactor

    目录 一.前言 二. Mono 与 Flux 构造器 三. 流计算 1. 缓冲 2. 过滤/提取 3. 转换 4. 合并 5. 合流 6. 累积 四.异常处理 五.线程调度 小结 参考阅读 一.前言 ...

  3. Reactive(3)5分钟理解 SpringBoot 响应式的核心-Reactor

    目录 一.前言 二. Mono 与 Flux 构造器 三. 流计算 1. 缓冲 2. 过滤/提取 3. 转换 4. 合并 5. 合流 6. 累积 四.异常处理 五.线程调度 小结 参考阅读 一.前言 ...

  4. Reactive 理解 SpringBoot 响应式的核心-Reactor

    Reactive 理解 SpringBoot 响应式的核心-Reactor bestcoding 2020-02-23 17:26:43 一.前言 关于 响应式 Reactive,前面的两篇文章谈了不 ...

  5. Spring Native实战(畅快体验79毫秒启动springboot应用)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. 解决 Springboot Unable to build Hibernate SessionFactory @Column命名不起作用

    问题: Springboot启动报错: Caused by: org.springframework.beans.factory.BeanCreationException: Error creati ...

  7. 【微框架】Maven +SpringBoot 集成 阿里大鱼 短信接口详解与Demo

    Maven+springboot+阿里大于短信验证服务 纠结点:Maven库没有sdk,需要解决 Maven打包找不到相关类,需要解决 ps:最近好久没有写点东西了,项目太紧,今天来一篇 一.本文简介 ...

  8. Springboot搭建web项目

    最近因为项目需要接触了springboot,然后被其快速零配置的特点惊呆了.关于springboot相关的介绍我就不赘述了,大家自行百度google. 一.pom配置 首先,建立一个maven项目,修 ...

  9. Java——搭建自己的RESTful API服务器(SpringBoot、Groovy)

    这又是一篇JavaWeb相关的博客,内容涉及: SpringBoot:微框架,提供快速构建服务的功能 SpringMVC:Struts的替代者 MyBatis:数据库操作库 Groovy:能与Java ...

随机推荐

  1. bzoj 2588: Spoj 10628. Count on a tree【主席树+倍增】

    算是板子,把值离散化,每个点到跟上做主席树,然后查询的时候主席树上用u+v-lca-fa[lca]的值二分 #include<iostream> #include<cstdio> ...

  2. Salazar Slytherin's Locket CodeForces - 855E

    Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...

  3. Codeforces Round #323 (Div. 2)

    被进爷坑了,第二天的比赛改到了12点 水 A - Asphalting Roads /************************************************ * Author ...

  4. Contextual Action bar(2) 简介,启动,各函数介绍

    一.Context Action Bar简介 它是一个ActionBar,有各种操作项,但它不是始终显示的ActionBar,它需要上下文才显示.样式如下: 二.Context Action Bar的 ...

  5. rac 添加 资源

    10g : 自动化.监控.os,存储,底成,网络,规范

  6. python中函数参数

    默认参数注意点 优点:灵活,当没有指定与形参对应的实参时就会使用默认参数 缺陷: 例子: >>> def h(m, l=[]):                    #默认参数时列 ...

  7. Asp.net 字符(一)

    1.字母大小写处理 private string GetChangedStr(string oldStr, strType type) { string newStr = ""; ...

  8. MySQL系列:utf8_bin和utf8_general_ci编码的区别

    MySQL中存在多种格式的utf8编码,其中最常见的两种为: utf8_bin utf8_general_ci utf8_bin将字符串中的每一个字符用二进制数据存储,区分大小写;utf8_gener ...

  9. Spring Boot整合Spring Batch

    引言 Spring Batch是处理大量数据操作的一个框架,主要用来读取大量数据,然后进行一定的处理后输出指定的形式.比如我们可以将csv文件中的数据(数据量几百万甚至几千万都是没问题的)批处理插入保 ...

  10. Git之master ->! [rejected] master (non-fast-forward)

    出现这个情况可能是在克隆项目的时候强制关闭或者是在pull的时候强制关闭 运行命令:git pull --rebase origin master 然后就可以 git push origin mast ...