詳解spring多線程與定時(shí)任務(wù)
本篇主要描述一下spring的多線程的使用與定時(shí)任務(wù)的使用.
1.spring多線程任務(wù)的使用
spring通過(guò)任務(wù)執(zhí)行器TaskExecutor來(lái)實(shí)現(xiàn)多線程與并發(fā)編程。通常使用ThreadPoolTaskExecutor來(lái)實(shí)現(xiàn)一個(gè)基于線程池的TaskExecutor.
首先你要實(shí)現(xiàn)AsyncConfigurer 這個(gè)接口,目的是開(kāi)啟一個(gè)線程池
代碼如下:
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; /** * 注入一個(gè)線程池 * @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; } }
然后注入一個(gè)類(lèi),實(shí)現(xiàn)你的業(yè)務(wù),并在你的Bean的方法中使用@Async注解來(lái)聲明其是一個(gè)異步任務(wù)
代碼如下:
package com.foreveross.service.weixin.test.thread; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** * 線程池任務(wù) * @author mingge * */ @Service public class TaskService { @Async public void executeAsyncTask(int i){ System.out.println("執(zhí)行異步任務(wù):"+i); } @Async public void executeAsyncTask1(int i){ System.out.println("執(zhí)行異步任務(wù)1:"+(i+i)); } }
最后通過(guò)測(cè)試,可以看到你的實(shí)現(xiàn)是異步執(zhí)行了.
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); } //最后可以根據(jù)結(jié)果可以看出結(jié)果是并發(fā)執(zhí)行而不是順序執(zhí)行的呢 context.close(); } }
2.spring定時(shí)任務(wù)的使用
在java原生態(tài)中,我們使用timer就可以了,這里小編說(shuō)一些在Spring中的定時(shí)任務(wù)的使用
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//開(kāi)啟對(duì)定時(shí)器的支持 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("我是定時(shí)任務(wù):"+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(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在SpringBoot中使用MongoDB完成數(shù)據(jù)存儲(chǔ)
本文主要介紹了在SpringBoot中如惡化使用MongoDB完成數(shù)據(jù)存儲(chǔ),接下來(lái)這篇我們將圍繞MongoDB進(jìn)行,MongoDB是一個(gè)開(kāi)源的,面向文檔的NoSQL數(shù)據(jù)庫(kù)管理系統(tǒng),使用類(lèi)似JSON的BSON(二進(jìn)制JSON)格式來(lái)存儲(chǔ)數(shù)據(jù),具有靈活的數(shù)據(jù)模型和強(qiáng)大的查詢(xún)功能,需要的朋友可以參考下2023-11-11Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Springboot發(fā)送郵件功能的實(shí)現(xiàn)詳解
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-09-0920秒教你學(xué)會(huì)java?List函數(shù)排序操作示例
這篇文章主要為大家介紹了20秒教你學(xué)會(huì)List函數(shù)排序操作示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09IntelliJ idea 如何生成動(dòng)態(tài)的JSON字符串(步驟詳解)
這篇文章主要介紹了IntelliJ idea 如何生成動(dòng)態(tài)的JSON字符串,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Java?-jar參數(shù)設(shè)置小結(jié)
本文主要介紹了Java?-jar參數(shù)設(shè)置小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06