国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

實(shí)例講解spring boot 多線程

瀏覽:123日期:2023-08-29 14:53:00

Spring 通過(guò)任務(wù)執(zhí)行器(TaskExecutor)來(lái)實(shí)現(xiàn)多線程和并發(fā)編程。使用ThreadPoolTaskExecutor可實(shí)現(xiàn)一個(gè)基于線程池的TaskExecutor。而實(shí)際開(kāi)發(fā)中任務(wù)一般是非阻塞的,即異步的,所有我們?cè)谂渲妙?lèi)中通過(guò)@EnableAsync開(kāi)啟對(duì)異步任務(wù)的支持,并通過(guò)在實(shí)際執(zhí)行的Bean的方法中使用@Async注解來(lái)聲明其是一個(gè)異步任務(wù)。

一、配置類(lèi)

package com.cenobitor.taskxecutor.config;import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;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;@Configuration@EnableAsyncpublic 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; }}

1、利用@EnableAsync注解開(kāi)啟異步任務(wù)支持

2、配置類(lèi)實(shí)現(xiàn)AsyncConfigurer接口并重寫(xiě)getAsyncExecutor方法,并返回一個(gè)ThreadPoolTaskExecutor,這樣我們就獲得了一個(gè)基于線程池TaskExecutor。

二、任務(wù)執(zhí)行類(lèi)

package com.cenobitor.taskxecutor.taskservice;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncTaskService { @Async public void excuteAsyncTask(Integer i){ System.out.println('異步執(zhí)行任務(wù):'+i); } @Async public void excuteAsyncTaskPlus(Integer i){ System.out.println('異步執(zhí)行任務(wù)+1:'+(i+1)); }}

通過(guò)@Async注解表明該方法是異步方法,如果注解在類(lèi)級(jí)別,則表明該類(lèi)所有的方法都是異步方法,而這里的方法自動(dòng)被注入使用ThreadPoolTaskExecutor作為T(mén)askExecutor。

如果在異步方法所在類(lèi)中調(diào)用異步方法,將會(huì)失效;

三、運(yùn)行

package com.cenobitor.taskxecutor;import com.cenobitor.taskxecutor.taskservice.AsyncTaskService;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskxecutorApplication.class); AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class); for (int i = 0; i < 10; i++) { asyncTaskService.excuteAsyncTask(i); asyncTaskService.excuteAsyncTaskPlus(i); } context.close(); }}

運(yùn)行結(jié)果:

異步執(zhí)行任務(wù):0異步執(zhí)行任務(wù)+1:1異步執(zhí)行任務(wù):1異步執(zhí)行任務(wù)+1:2異步執(zhí)行任務(wù):2異步執(zhí)行任務(wù):3異步執(zhí)行任務(wù):5異步執(zhí)行任務(wù)+1:6異步執(zhí)行任務(wù):6異步執(zhí)行任務(wù)+1:7異步執(zhí)行任務(wù):7異步執(zhí)行任務(wù)+1:8異步執(zhí)行任務(wù):8異步執(zhí)行任務(wù)+1:9異步執(zhí)行任務(wù):9異步執(zhí)行任務(wù)+1:10異步執(zhí)行任務(wù)+1:3異步執(zhí)行任務(wù):4異步執(zhí)行任務(wù)+1:5異步執(zhí)行任務(wù)+1:4

注:摘抄自《JavaEE開(kāi)發(fā)的顛覆者SpringBoot 實(shí)戰(zhàn)》。

以上就是實(shí)例講解spring boot 多線程的詳細(xì)內(nèi)容,更多關(guān)于spring boot 多線程的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 溆浦县| 武功县| 广安市| 日土县| 息烽县| 望谟县| 清苑县| 马龙县| 江陵县| 宝山区| 土默特左旗| 从江县| 黄浦区| 云南省| 广安市| 宜兴市| 都安| 安远县| 朝阳县| 昌邑市| 柘荣县| 惠水县| 磐石市| 松阳县| 崇明县| 龙泉市| 祁东县| 双辽市| 武强县| 乳山市| 衡阳县| 红桥区| 龙胜| 汝南县| 竹北市| 太湖县| 建始县| 合山市| 怀化市| 正定县| 二连浩特市|