package com.foreveross.service.weixin.test.thread;
import java.util.concurrent.Executor;
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;
/**
* 注入一个线程池
* @author mingge
*
*/
@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor=new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(20);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
package com.foreveross.service.weixin.test.thread;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* 线程池任务
* @author mingge
*
*/
@Service
public class TaskService {
@Async
public void executeAsyncTask(int i){
System.out.println("执行异步任务:"+i);
}
@Async
public void executeAsyncTask1(int i){
System.out.println("执行异步任务1:"+(i+i));
}
}
package com.foreveross.service.weixin.test.thread;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*
* @author mingge
*
*/
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
TaskService taskService=context.getBean(TaskService.class);
for(int i=0;i<20;i++){
taskService.executeAsyncTask(i);
taskService.executeAsyncTask1(i);
}
//最后可以根据结果可以看出结果是并发执行而不是顺序执行的呢
context.close();
}
}
package com.foreveross.service.weixin.test.thread;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@ComponentScan("com.foreveross.service.weixin.test.thread")
@EnableScheduling//开启对定时器的支持
public class TaskSchedulerConfig {
}
package com.foreveross.service.weixin.test.thread;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class TimerTaskJob {
@Scheduled(fixedRate=2000)
public void test(){
System.out.println("我是定时任务:"+new Date().getSeconds());
}
}
package com.foreveross.service.weixin.test.thread;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestTimer {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
//context.close();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有