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 ...
随机推荐
- 学到了林海峰,武沛齐讲的Day34 完 线程 进程 协程 很重要
线程 进程 协程 很重要 ...儿子满月回家办酒,学的有点慢,坚持
- 洛谷 P1854 花店橱窗布置 题解
Analysis 给定一个f*v的矩阵 要求从第一行走到第f行,每行取走一个数, 且该行所取的数必须必上一行所取的数的列数大 , 求所能取走的最大值 注意每一行所取走的数字的列数必须大于等该行的行号 ...
- linux 环境变量的设置
方法一: 在/etc/profile文件中添加变量[对所有用户生效(永久的)] 用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久的”. 要让刚才的 ...
- 微信小程序开源
| UI组件 | | | | | | | | | weui-wxss ★1873 - 同微信原生视觉体验一致的基础样式库 | | | | | | zanui-weapp ★794 - 好用易扩展的小程 ...
- (23)打鸡儿教你Vue.js
实例: 模板语法 vue-router,vuex以及调式方法介绍 打包部署: npm run build Webpack 目前无论在求职还是工作中,使用越来越普及.而想要学懂,学会Webpack更绝非 ...
- 1071 Speech Patterns (25)(25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- BS4库详解
from bs4 import BeautifulSoup html = """ <html><head><title>This is ...
- 深度学习面试题25:分离卷积(separable卷积)
目录 举例 单个张量与多个卷积核的分离卷积 参考资料 举例 分离卷积就是先在深度上分别卷积,然后再进行卷积,对应代码为: import tensorflow as tf # [batch, in_he ...
- Audit(二)--清理Audit数据
(一) 概述 Audit的数据主要存储在sys.aud$表中,该表默认位于system表空间中,我们根据需求,将该表移到了sysaux表空间中.由于审计数据量较大,需要经常关注sysaux表空间的使用 ...
- laravel修改了配置文件不生效,修改了数据库配置文件不生效
Laravel缓存配置文件,因此您可能只需要清除缓存: php artisan config:clear 转: http://www.voidcn.com/article/p-sgcusrjp-bxw ...