详解spring多线程与定时任务
本篇主要描述一下spring的多线程的使用与定时任务的使用.
1.spring多线程任务的使用
spring通过任务执行器TaskExecutor来实现多线程与并发编程。通常使用ThreadPoolTaskExecutor来实现一个基于线程池的TaskExecutor.
首先你要实现AsyncConfigurer这个接口,目的是开启一个线程池
代码如下:
packagecom.foreveross.service.weixin.test.thread; importjava.util.concurrent.Executor; importorg.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; importorg.springframework.scheduling.annotation.AsyncConfigurer; importorg.springframework.scheduling.annotation.EnableAsync; importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; /** *注入一个线程池 *@authormingge * */ @Configuration @ComponentScan("com.foreveross.service.weixin.test.thread") @EnableAsync publicclassTaskExecutorConfigimplementsAsyncConfigurer{ @Override publicExecutorgetAsyncExecutor(){ ThreadPoolTaskExecutortaskExecutor=newThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(20); taskExecutor.setQueueCapacity(25); taskExecutor.initialize(); returntaskExecutor; } @Override publicAsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler(){ returnnull; } }
然后注入一个类,实现你的业务,并在你的Bean的方法中使用@Async注解来声明其是一个异步任务
代码如下:
packagecom.foreveross.service.weixin.test.thread; importorg.springframework.scheduling.annotation.Async; importorg.springframework.stereotype.Service; /** *线程池任务 *@authormingge * */ @Service publicclassTaskService{ @Async publicvoidexecuteAsyncTask(inti){ System.out.println("执行异步任务:"+i); } @Async publicvoidexecuteAsyncTask1(inti){ System.out.println("执行异步任务1:"+(i+i)); } }
最后通过测试,可以看到你的实现是异步执行了.
packagecom.foreveross.service.weixin.test.thread; importorg.springframework.context.annotation.AnnotationConfigApplicationContext; /** * *@authormingge * */ publicclassTest{ publicstaticvoidmain(String[]args){ AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(TaskExecutorConfig.class); TaskServicetaskService=context.getBean(TaskService.class); for(inti=0;i<20;i++){ taskService.executeAsyncTask(i); taskService.executeAsyncTask1(i); } //最后可以根据结果可以看出结果是并发执行而不是顺序执行的呢 context.close(); } }
2.spring定时任务的使用
在java原生态中,我们使用timer就可以了,这里小编说一些在Spring中的定时任务的使用
packagecom.foreveross.service.weixin.test.thread; importorg.springframework.context.annotation.ComponentScan; importorg.springframework.context.annotation.Configuration; importorg.springframework.scheduling.annotation.EnableScheduling; @Configuration @ComponentScan("com.foreveross.service.weixin.test.thread") @EnableScheduling//开启对定时器的支持 publicclassTaskSchedulerConfig{ }
packagecom.foreveross.service.weixin.test.thread; importjava.util.Date; importorg.springframework.scheduling.annotation.Scheduled; importorg.springframework.stereotype.Service; @Service publicclassTimerTaskJob{ @Scheduled(fixedRate=2000) publicvoidtest(){ System.out.println("我是定时任务:"+newDate().getSeconds()); } }
packagecom.foreveross.service.weixin.test.thread; importorg.springframework.context.annotation.AnnotationConfigApplicationContext; publicclassTestTimer{ publicstaticvoidmain(String[]args){ AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext(TaskSchedulerConfig.class); //context.close(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。