1.pom依赖

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>

2.编写异步方法

package com.yun.base.custom.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; @Component
public class AsyncThread { private static final Logger LOGGER = LoggerFactory.getLogger(AsyncThread.class); @Async
public void runMethodAsync() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法1执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Async
public void runMethodAsync2() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法2执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} @Async
public void runMethodAsync3() {
Thread t = Thread.currentThread();
try {
LOGGER.debug("异步方法3执行中");
t.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

2.配置线程池及开启注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="true"> <context:component-scan base-package="com.yun.base.custom.event" /> <task:executor id="eventExecutor" pool-size="2" />
<task:annotation-driven executor="eventExecutor"/> </beans>

3.测试

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
package test.war;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.yun.base.custom.event.AsyncThread; @RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations = { "classpath:conf/async.xml" } )
public class JunitTest { private static final Logger LOGGER = LoggerFactory.getLogger(JunitTest.class); @Autowired
private ApplicationContext applicationContext; @Test
public void asyncTest() {
AsyncThread asyncThread = (AsyncThread) applicationContext.getBean("asyncThread"); LOGGER.debug("开始调用");
asyncThread.runMethodAsync();
asyncThread.runMethodAsync2();
asyncThread.runMethodAsync3();
LOGGER.debug("结束调用"); try {
Thread.currentThread().sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
} } }

4.结果分析

2017-09-11 15:05:34.323 [main] DEBUG test.war.JunitTest - 开始调用
2017-09-11 15:05:34.338 [main] DEBUG test.war.JunitTest - 结束调用
2017-09-11 15:05:34.370 [eventExecutor-1] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法1执行中
2017-09-11 15:05:34.370 [eventExecutor-2] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法2执行中
2017-09-11 15:05:44.378 [eventExecutor-2] DEBUG com.yun.base.custom.event.AsyncThread - 异步方法3执行中

可以看到在主线程 main 下执行完成之后,另开启了2个线程执行异步方法1,2,然后10秒之后线程2继续执行异步方法3。

这里我设置的线程数为2,所以第一论执行的时候,同时只能有2个线程来执行异步方法,异步方法3等待,当有线程释放的时候继续执行异步方法3。

Spring异步调用注解@Async的使用的更多相关文章

  1. 如何在项目中使用Spring异步调用注解@Async

    本文主要介绍如何使用Spring框架提供的异步调用注解@Async,异步线程池配置.异常捕获处理. 开启@Async注解支持 使用@Async注解的之前,必须在项目中启动时调用@EnableAsync ...

  2. Spring异步调用原理及SpringAop拦截器链原理

    一.Spring异步调用底层原理 开启异步调用只需一个注解@EnableAsync @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTI ...

  3. Spring Boot中实现异步调用之@Async

    一.什么是异步调用 “异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行:异步调用指程序在顺序执行时,不等待异步调用 的语句返回结果 ...

  4. 161107、spring异步调用,完美解决!

    项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步的方式 ...

  5. 161021、spring异步调用,完美解决!

    前言 项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步 ...

  6. 如何在Spring异步调用中传递上下文

    以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...

  7. Spring异步任务处理,@Async的配置和使用

    本文转自http://blog.csdn.net/clementad/article/details/47403185 感谢作者 这个注解用于标注某个方法或某个类里面的所有方法都是需要异步处理的.被注 ...

  8. spring boot 学习(十一)使用@Async实现异步调用

    使用@Async实现异步调用 什么是”异步调用”与”同步调用” “同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行:”异步调用”则是只要上一行代码执行,无需等 ...

  9. SpringBoot | 第二十一章:异步开发之异步调用

    前言 上一章节,我们知道了如何进行异步请求的处理.除了异步请求,一般上我们用的比较多的应该是异步调用.通常在开发过程中,会遇到一个方法是和实际业务无关的,没有紧密性的.比如记录日志信息等业务.这个时候 ...

随机推荐

  1. BGP - 5,BGP属性

    metric,自己决定去哪个EBGP邻居 local-pre,影响AS内部IBGP邻居的路由决策 med,影响AS外部EBGP邻居的路由决策   1,BGP属性     公认传递(well-known ...

  2. 20170821xlVBA跨表公式套用

    Public Sub CopyModelHideBlankRows() AppSettings Dim StartTime As Variant Dim UsedTime As Variant Sta ...

  3. leetcode-algorithms-14 Longest Common Prefix

    leetcode-algorithms-14 Longest Common Prefix Write a function to find the longest common prefix stri ...

  4. python 小练习 5

    Py从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992, 这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和 ...

  5. CSS text-decoration 属性

    定义和用法 text-decoration 属性规定添加到文本的修饰. 注释:修饰的颜色由 "color" 属性设置. 说明 这个属性允许对文本设置某种效果,如加下划线.如果后代元 ...

  6. 1004. Max Consecutive Ones III最大连续1的个数 III

    网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...

  7. Hadoop---日志服务器

    Hadoop---日志服务器 1.历史服务器: 启动: 1)测试使用: 1.做一个任务: 2.接受任务: 3.执行任务: 4.执行任务完成: 2)日志文件产生: 成功后:   失败后: 点击进入His ...

  8. 音频 API 一览

    iOS 和 OS X 平台都有一系列操作音频的 API,其中涵盖了从低到高的全部层级.随着时间的推移.平台的增长以及改变,不同 API 的数量可以说有着非常巨大的变化.本文对当前可以使用的 API 以 ...

  9. vue-navigation 实现前进刷新,后退不刷新

    vue-navigation GitHub地址 导航默认行为类似手机APP的页面导航(A.B.C为页面): A前进到B,再前进到C: C返回到B时,B会从缓存中恢复: B再次前进到C,C会重新生成,不 ...

  10. Qt Widgets——主窗口及其主要组成部分

    Main Window and Related Classes QAction 动作类,用于当做一个菜单项或工具项插入菜单或工具栏 QActionGroup 动作组,用于管理多个动作,设置它们之间的互 ...