package com.example.demo_3_2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* Created by WangBin on 2017/4/18.
*
*/
@Service
public class AsyncTaskService {
@Async//通过该注解 表明 该方法是个异步方法如果注解在类级别 则表明 该类所有的方法都是异步方法
// 而这里的方法自动被注入使用 ThreadPoolTaskExecutor 作为TaskExecutor
public void executeAsyncTask(Integer i){
System.err.println("执行异步任务====="+i);
}
@Async
public void executeAsyncTaskPlus(Integer i){
System.err.println("执行异步任务+1====="+(i+1));
}
}
package com.example.demo_3_2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by WangBin on 2017/4/18.
*
*/
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
for (int i = 0; i < 10; i++) {
asyncTaskService.executeAsyncTask(i);
asyncTaskService.executeAsyncTaskPlus(i);
}
context.close();
}
}