1.pom依賴spring
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.9.RELEASE</version> </dependency>
2.編寫異步方法app
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.測試async
<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執行中spa
能夠看到在主線程 main 下執行完成以後,另開啓了2個線程執行異步方法1,2,而後10秒以後線程2繼續執行異步方法3。線程
這裏我設置的線程數爲2,因此第一論執行的時候,同時只能有2個線程來執行異步方法,異步方法3等待,當有線程釋放的時候繼續執行異步方法3。debug