莫大人

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;

@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。

 

posted on 2017-09-11 15:09  莫大人  阅读(5317)  评论(0编辑  收藏  举报

导航