成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

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

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

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

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

一、配置類

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注解開啟異步任務(wù)支持

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

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

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)); }}

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

如果在異步方法所在類中調(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開發(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)文章: