转发: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的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  4. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

  5. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  6. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

  7. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  8. 12点睛Spring4.1-Spring Aware

    12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...

  9. 11点睛Spring4.1-Property Editor

    11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...

随机推荐

  1. keepalived 的 vrrp_script

    [root@centos01 keepalived]# cat check_httpd.sh 脚本需要有执行权限 通常情况下,利用keepalived做热备,其中一台设置为master,一台设置为ba ...

  2. dict 的 items() 方法与 iteritems() 方法的不同?

    items方法将所有的字典以列表方式返回,其中项在返回时没有特殊的顺序: iteritems方法有相似的作用,但是返回一个迭代器对象

  3. 【洛谷】P1275 魔板(暴力&思维)

    题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状态.操作的方式有两 ...

  4. 数据结构Java版之递归与迭代算法(五)

    递归的概念很简单,就是自己调用自己. 而迭代,则是通过修改初始化数据,得到中间结果,然后不断的对中间结果进行修改,而得到最终结果.简单来说迭代就是循环. 在此,我们用一个比较经典的Fibonacci数 ...

  5. Gremlin入门

    Gremlin入门 一.Gremlin简介 Gremlin是Apache ThinkerPop框架下的图遍历语言,Gremlin是一种函数式数据流语言,可以使用户使用简洁的方式表述复杂的属性图的遍历或 ...

  6. 大数据技术之kettle(2)——练习三个基本操作

    一.同一数据库两表数据关联更新 实现效果:把stu1的数据按id同步到stu2,stu2有相同id则更新数据 步骤: 1.在mysql中创建两张表: mysql>create database ...

  7. 使用adb 命令(atrace)抓起systrace的方法。【转】

    本文转载自:https://www.cnblogs.com/liuliu-word/p/9963017.html adb shell atrace -c -b 10240 --async_start ...

  8. C# 获取USB设备信息

    C# 获取USB设备信息WMI方式 using System; using System.Management; using System.Text.RegularExpressions; using ...

  9. pt-table-checksum校验与pt-table-sync修复数据【转】

    1:下载工具包 登录网站下载相应的工具包 https://www.percona.com/downloads/percona-toolkit/LATEST/ 2:安装 (1)yum安装: sudo y ...

  10. 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_38、源码编译安装Redis4.x

    笔记 2.源码编译安装Redis4.x     简介:使用源码安装Redis4.x和配置外网访问 1.快速安装  https://redis.io/download#installation      ...