多线程
- Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程。
- 使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor
- 一般情况下,面对的都是异步任务,要在配置类中使用
@EnableAsync
开启对异步任务的支持 - 并通过在实际执行的Bean的方法中,使用
@Async
注解来声明其是一个异步任务
示例
1,配置类
package com.zyf;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* Created by zyf on 2018/3/5.
*/
@Configuration
@ComponentScan(value = "com.zyf")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
2,待执行的Bean
package com.zyf;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* Created by zyf on 2018/3/5.
*/
@Service
public class AsyncTaskService {
/**
* 通过@Async注解,表示该方法是一个异步方法
* 如果将@Async注解标识在类的声明上,则表明这个类中所有的方法都是异步的
* 异步方法,会被自动注入在TaskExecutorConfig类中返回的
* 那个ThreadPoolTaskExecutor作为TaskExecutor
* @param n
*/
@Async
public void executeAsyncTask(Integer n){
System.out.println("异步任务执行:"+n);
}
@Async
public void executeAsyncTaskPlus(Integer n){
System.out.println("异步任务执行+1:"+(n+1));
}
}
3,测试类
package com.zyf;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by zyf on 2018/3/5.
*/
public class AsyncMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.register(TaskExecutorConfig.class);
context.refresh();
AsyncTaskService taskService = context.getBean(AsyncTaskService.class);
for (int i = 0; i < 10; i++) {
taskService.executeAsyncTask(i);
taskService.executeAsyncTaskPlus(i);
}
context.close();
}
}
4,测试结果