当前位置: 首页 >服务端 > Spring异步调用注解@Async的使用

Spring异步调用注解@Async的使用

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;@Componentpublic class AsyncThread {private static final Logger LOGGER = LoggerFactory.getLogger(AsyncThread.class);@Asyncpublic void runMethodAsync() {Thread t = Thread.currentThread();try {LOGGER.debug("异步方法1执行中");t.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}@Asyncpublic void runMethodAsync2() {Thread t = Thread.currentThread();try {LOGGER.debug("异步方法2执行中");t.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}@Asyncpublic 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsdhttp://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);@Autowiredprivate ApplicationContext applicationContext;@Testpublic 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。

 

来源链接:https://www.cnblogs.com/yun965861480/p/7505112.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/112156.html

标签:Spring注解
分享给朋友:

“Spring异步调用注解@Async的使用” 的相关文章