Spring创建任务执行器实现并发
2018-04-03 15:53
134 查看
### 配置类
@Configuration
@ComponentScan("demo")
@EnableAsync
public class Config implements AsyncConfigurer{
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(26);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
### 服务层
@org.springframework.stereotype.Service
public class Service {
@Async
public void task(Integer i){
System.out.println("异步"+i);
}
@Async
public void task1(Integer i){
System.out.println("2异步"+i);
}
}
### 测试层
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
Config.class);
Service s = annotationConfigApplicationContext.getBean(Service.class);
for (int i = 0; i < 10; i++) {
s.task(i);
s.task1(i);
}
}
}
### 结果
异步1
异步0
异步3
2异步0
异步4
2异步4
异步5
2异步5
异步6
2异步6
异步7
2异步7
异步8
2异步8
异步9
2异步9
2异步3
2异步2
2异步1
异步2
读取属性文件,配置类型安全的bean
@Component
@ConfigurationProperties(prefix="book",location={"classpath:config/book.properties"})
### restful API
> 获取请求路径中?后面的参数@RequestParam("id")
> 获取路径中/1/的1 @PathVariable
### snowflake算法
@Configuration
@ComponentScan("demo")
@EnableAsync
public class Config implements AsyncConfigurer{
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(26);
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
### 服务层
@org.springframework.stereotype.Service
public class Service {
@Async
public void task(Integer i){
System.out.println("异步"+i);
}
@Async
public void task1(Integer i){
System.out.println("2异步"+i);
}
}
### 测试层
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
Config.class);
Service s = annotationConfigApplicationContext.getBean(Service.class);
for (int i = 0; i < 10; i++) {
s.task(i);
s.task1(i);
}
}
}
### 结果
异步1
异步0
异步3
2异步0
异步4
2异步4
异步5
2异步5
异步6
2异步6
异步7
2异步7
异步8
2异步8
异步9
2异步9
2异步3
2异步2
2异步1
异步2
读取属性文件,配置类型安全的bean
@Component
@ConfigurationProperties(prefix="book",location={"classpath:config/book.properties"})
### restful API
> 获取请求路径中?后面的参数@RequestParam("id")
> 获取路径中/1/的1 @PathVariable
### snowflake算法
相关文章推荐
- spring与quart整合实现任务调度_学习笔记
- Spring定时任务的几种实现
- quartz结合spring轻松实现任务调度
- Spring与Quartz的整合实现定时任务调度
- Quartz学习笔记(七) quartz与spring实现任务动态管理面板
- SpringBoot 多任务并行+线程池处理的实现
- Spring task quartz 定时任务的几种实现
- springboot整合Quartz实现动态配置定时任务
- 使用Spring的@Scheduled实现定时任务
- 实现定时任务,包括修改执行策略,用quartz+Spring
- quartz+spring 实现多任务动态定时器问题
- 【Spring源码--AOP的实现】(一)AopProxy代理对象的创建
- 详解Spring整合Quartz实现动态定时任务
- Spring定时任务的几种实现
- Spring定时任务的几种实现
- springboot整合Quartz实现动态配置定时任务
- 【译】Spring 4 基于TaskScheduler实现定时任务(注解)
- spring实现定时任务的两种方式
- Spring和Quartz实现的定时任务调度