应用场景:

1、某些耗时较长的而用户不需要等待该方法的处理结果

2、某些耗时较长的方法,后面的程序不需要用到这个方法的处理结果时

在spring的配置文件中加入对异步执行的支持

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <context:annotation-config />
<!--扫描注解 -->
<context:component-scan base-package="com.tf" />
<!-- 支持异步方法执行 -->
<task:annotation-driven />

使用方法

import org.springframework.scheduling.annotation.Async;

public class Test {

    @Async
public static void testAsyncMethod(){
try {
//让程序暂停100秒,相当于执行一个很耗时的任务
Thread.sleep(100000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

调用方法

public static void main(String[] args) {
Test.testAsyncMethod();
System.out.println("我已经执行了!");
}

使用spring的@Async异步执行方法的更多相关文章

  1. Spring Boot Async异步执行

    异步调用就是不用等待结果的返回就执行后面的逻辑,同步调用则需要等带结果再执行后面的逻辑. 通常我们使用异步操作都会去创建一个线程执行一段逻辑,然后把这个线程丢到线程池中去执行,代码如下: Execut ...

  2. 使用SpringMVC @Async异步执行方法的笔记 (转载)

    原文:http://blog.csdn.net/yuwenruli/article/details/8514393 测试代码: @RunWith(SpringJUnit4ClassRunner.cla ...

  3. Spring Boot 之异步执行方法

    前言: 最近的时候遇到一个需求,就是当服务器接到请求并不需要任务执行完成才返回结果,可以立即返回结果,让任务异步的去执行.开始考虑是直接启一个新的线程去执行任务或者把任务提交到一个线程池去执行,这两种 ...

  4. springboot学习入门简易版七---springboot2.0使用@Async异步执行方法(17)

    1启动类开启异步调用注解 @SpringBootApplication @EnableAsync //开启异步调用 public class StartApplication { 不开启则异步调用无效 ...

  5. C#.NET使用Task,await,async,异步执行控件耗时事件(event),不阻塞UI线程和不跨线程执行UI更新,以及其他方式比较

    使用Task,await,async,异步执行事件(event),不阻塞UI线程和不跨线程执行UI更新 使用Task,await,async 的异步模式 去执行事件(event) 解决不阻塞UI线程和 ...

  6. Spring Boot @Async 异步任务执行

    1.任务执行和调度 Spring用TaskExecutor和TaskScheduler接口提供了异步执行和调度任务的抽象. Spring的TaskExecutor和java.util.concurre ...

  7. 利用Spring的@Async异步处理改善web应用中耗时操作的用户体验

    Web应用中,有时会遇到一些耗时很长的操作(比如:在后台生成100张报表再呈现,或 从ftp下载若干文件,综合处理后再返回给页面下载),用户在网页上点完按钮后,通常会遇到二个问题:页面超时.看不到处理 ...

  8. spring boot @Async异步注解上下文透传

    上一篇文章说到,之前使用了@Async注解,子线程无法获取到上下文信息,导致流量无法打到灰度,然后改成 线程池的方式,每次调用异步调用的时候都手动透传 上下文(硬编码)解决了问题. 后面查阅了资料,找 ...

  9. 使用Task异步执行方法_多线程_应用程序池

    偶然遇到在执行登录的方法需要发送消息队列导致登录时间过长的问题,从网上查了一些方法,先将一个简单的异步处理程序的小例子展示出来,供大家参考: 备注:该方法是从应用程序程序所在的线程池中获取线程,第一次 ...

随机推荐

  1. asp.net MVC 3/4 equivalent to a response.filter

    am in a need to intercept all of the html that will be sent to the browser and replace some tags tha ...

  2. 使用 NSPropertyListSerialization 持久化字典与数组

    NSPropertyListSerialization The NSPropertyListSerialization class provides methods that convert prop ...

  3. 让子弹飞Demo版

    让子弹飞是我非常喜欢的一款游戏.今天的目标就是利用cocos2dx 3.0 和box2d 打造一款这样的类型游戏的Demo版.本来cocos2dx 3.0 已经封装了physicals模块,可是我在使 ...

  4. Excel 批量快速合并相同的单元格:数据透视表、宏代码、分类汇总

    Excel 批量快速合并相同的单元格   在制作Excel表格的时候,为了使得自己制作的报表更加简洁明了,方便查阅,经常需要合并很多相同的单元格,如果有几千几万条记录需要合并的话,真的会让人发疯.怎样 ...

  5. python限制进程、子进程占用内存大小、CPU时间的方法:resource模块

    内置模块:resource 在mac环境下功能会存在问题.linux下可以使用:但是for i in range(10000)的值必须是10000或者更大的数值才有用.没有搞清楚为什么 #/usr/b ...

  6. C++类模板的声明和定义为什么要放在同一个文件

    不是只能放在.h里面,但是推荐放在.h里面.STL模板实现全部是放在.h里面的.------------------编译能通过.1)参与编译的只是.cpp文件,不会报错的原因,是因为它能在.h里面找到 ...

  7. Informatica 常用组件Lookup之五 转换属性

    查找转换的属性标识数据库源.PowerCenter 如何处理转换,以及它如何处理高速缓存和多项匹配. 创建映射时,为每个查找转换指定属性.创建会话时,您可在会话属性中覆盖某些属性,如每个转换的索引和数 ...

  8. waf bypass

    1.前言 去年到现在就一直有人希望我出一篇关于waf绕过的文章,我觉得这种老生常谈的话题也没什么可写的.很多人一遇到waf就发懵,不知如何是好,能搜到的各种姿势也是然并卵.但是积累姿势的过程也是迭代的 ...

  9. 基于zabbix 的memached 多实例监控

    基于zabbix 的memached 多实例监控 zabbix agentd 配置文件新增配置: UserParameter=memcached.server.discovery[*],ps uax ...

  10. 【属性动画总结】Property Animation

    属性动画概述 3.0以前,android仅支持两种动画模式,tweened animation 和 frame-by-frame animation,在android3.0中又引入了一个新的动画系统: ...