15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679
15.1 TaskExecutor
- spring的TaskExecutor为在spring环境下进行并发的多线程编程提供了支持;
- 使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor;
- 使用@EnableAsync开启异步任务支持;
- 使用@Async注解方法是异步方法;
15.2 示例
15.2.1 声明taskExecutor
package com.wisely.task.executor; import java.util.concurrent.Executor; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @Configuration
@EnableAsync
public class DemoConfig implements AsyncConfigurer{
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
} public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
} }
15.2.2 异步任务实现代码
package com.wisely.task.executor; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
public class DemoAsyncTask {
@Async
public void executeAsyncTask(Integer i){
System.out.println("执行异步任务:"+i);
} @Async
public void executeAsyncTaskPlus(Integer i){
System.out.println("执行异步任务+1:"+(i+1);
}
}
15.2.3 测试
package com.wisely.task.executor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.task.executor");
DemoAsyncTask task = context.getBean(DemoAsyncTask.class);
for(int i =0 ;i<10;i++){
task.executeAsyncTask(i);
task.executeAsyncTaskPlus(i);
}
context.close();
}
}
输出结果(结果是并发执行而不是顺序执行的):
执行异步任务+1:10
执行异步任务:6
执行异步任务:4
执行异步任务+1:7
执行异步任务:3
执行异步任务:1
执行异步任务:5
执行异步任务:7
执行异步任务+1:8
执行异步任务:8
执行异步任务+1:9
执行异步任务:9
执行异步任务+1:1
执行异步任务:0
执行异步任务+1:2
执行异步任务+1:3
执行异步任务:2
执行异步任务+1:4
执行异步任务+1:5
执行异步任务+1:6
15点睛Spring4.1-TaskExecutor的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 12点睛Spring4.1-Spring Aware
12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...
- 11点睛Spring4.1-Property Editor
11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...
随机推荐
- DBA 有哪些工作
首先,我们看看DBA的工作有哪些?DBA的工作实际上都是围绕数据库展开,包含但不限于这些工作: 1. 数据库.主机.操作系统.交换机.存储选型,预算,架构设计,部署,参数优化: 2. 数据库备份.恢复 ...
- UVA 11468 Substring (记忆化搜索 + AC自动鸡)
传送门 题意: 给你K个模式串, 然后,再给你 n 个字符, 和它们出现的概率 p[ i ], 模式串肯定由给定的字符组成. 且所有字符,要么是数字,要么是大小写字母. 问你生成一个长度为L的串,不包 ...
- 【JS】知识笔记
一.JS文件位置 多个.JS文件最好合并到一个文件中,减少加载页面时发送的请求数量. 某个单独页面需要的js,最好放在HTML文档的最后,</body>之前,这样能使浏览器更快地加载页面. ...
- Linux环境下面安装Perl环境
1.下载安装 wget http://www.cpan.org/src/5.0/perl-5.26.1.tar.gz tar zxvf perl-5.26.1.tar.gz cd perl-5.26. ...
- php技能树---大神的进阶之路
PHP7 迎来巨大的性能提升,又一次回到关注的焦点.根据这些年在开发圈子总结的LNMP程序猿发展轨迹,结合个人经验体会,总结出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段 ...
- sed、awk命令速查
awk与sed.grep一样都是为了加工数据流而做成的文本加工过滤器命令.awk会事先把输入的数据根据字段单位进行分割.在没有指定分割单位的情况下,以输入数据中的空格或Tab为分隔符.与sed相比,它 ...
- redis渐进式 rehash
转载(http://redisbook.com/preview/dict/incremental_rehashing.html) 上一节说过, 扩展或收缩哈希表需要将 ht[0] 里面的所有键值对 r ...
- ubuntu之路——day4(今天主要看了神经网络的概念)
感谢两位老师做的免费公开课: 第一个是由吴恩达老师放在网易云课堂的神经网络和深度学习,比较偏理论,使用numpy包深入浅出的介绍了向量版神经网络的处理方式,当然由于视频有点老,虽然理论很好但是工具有点 ...
- java实现获取当前年、月、日 、小时 、分钟、 秒、 毫秒
转载 : https://blog.csdn.net/qq_36652619/article/details/85621020 package com.app.test; import java.te ...
- Barman安装及备份PostgreSQL
barman特点 零数据丢失备份.保证用户在只有一台备份服务器的情况下达到零数据丢失. 与备份服务器合作.允许备份服务器在与主服务器的流式复制不可用时,从barman获取wal文件. 可靠的监控集成. ...