Java

Springboot对多线程的支持详解(转载)

2018-07-25  本文已影响21人  Java架构技术分享

作者:青春无罪

原文链接:https://segmentfault.com/a/1190000015766938

Springboot对多线程的支持详解

这两天看阿里的JAVA开发手册,到多线程的时候说永远不要用 new Thread()这种方式来使用多线程。确实是这样的,我一直在用线程池,到了springboot才发现他已经给我们提供了很方便的线程池机制。

本博客代码托管在github上https://github.com/gxz0422042...

一、介绍

Spring是通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor来创建一个基于线城池的TaskExecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@EnableAsync可以开启异步任务。然后在实际执行的方法上配置注解@Async上声明是异步任务。

二、配置类

配置类代码如下:

packagecom.spartajet.springbootlearn.thread;importorg.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.AsyncConfigurer;importorg.springframework.scheduling.annotation.EnableAsync;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;importjava.util.concurrent.Executor;/** *@description*@create2017-02-22 下午11:53 *@emailgxz04220427@163.com */@Configuration@EnableAsyncpublicclassThreadConfigimplementsAsyncConfigurer{/**    * The {@linkExecutor} instance to be used when processing async    * method invocations.    */@OverridepublicExecutorgetAsyncExecutor(){        ThreadPoolTaskExecutor executor =newThreadPoolTaskExecutor();        executor.setCorePoolSize(5);        executor.setMaxPoolSize(15);        executor.setQueueCapacity(25);        executor.initialize();returnexecutor;    }/**    * The {@linkAsyncUncaughtExceptionHandler} instance to be used    * when an exception is thrown during an asynchronous method execution    * with {@codevoid} return type.    */@OverridepublicAsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler(){returnnull;    }}

解读:

利用EnableAsync来开启Springboot对于异步任务的支持

配置类实现接口AsyncConfigurator,返回一个ThreadPoolTaskExecutor线程池对象。

三、任务执行

任务执行代码:

packagecom.spartajet.springbootlearn.thread;importorg.springframework.scheduling.annotation.Async;importorg.springframework.stereotype.Service;/**

* @description

* @create 2017-02-23 上午12:00

* @email gxz04220427@163.com

*/@Servicepublic class AsyncTaskService {    @Asyncpublic void executeAsyncTask(int i) {System.out.println("线程" +Thread.currentThread().getName() + " 执行异步任务:" +i);    }}

代码解读:

通过@Async注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。

四、测试代码

packagecom.spartajet.springbootlearn;importcom.spartajet.springbootlearn.thread.AsyncTaskService;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublicclassSpringbootLearnApplicationTests{@AutowiredprivateAsyncTaskService asyncTaskService;@TestpublicvoidcontextLoads(){    }@TestpublicvoidthreadTest(){for(inti =0; i <20; i++) {            asyncTaskService.executeAsyncTask(i);        }    }}

测试结果:

线程ThreadPoolTaskExecutor-4执行异步任务:3线程ThreadPoolTaskExecutor-2执行异步任务:1线程ThreadPoolTaskExecutor-1执行异步任务:0线程ThreadPoolTaskExecutor-1执行异步任务:7线程ThreadPoolTaskExecutor-1执行异步任务:8线程ThreadPoolTaskExecutor-1执行异步任务:9线程ThreadPoolTaskExecutor-1执行异步任务:10线程ThreadPoolTaskExecutor-5执行异步任务:4线程ThreadPoolTaskExecutor-3执行异步任务:2线程ThreadPoolTaskExecutor-5执行异步任务:12线程ThreadPoolTaskExecutor-1执行异步任务:11线程ThreadPoolTaskExecutor-2执行异步任务:6线程ThreadPoolTaskExecutor-4执行异步任务:5线程ThreadPoolTaskExecutor-2执行异步任务:16线程ThreadPoolTaskExecutor-1执行异步任务:15线程ThreadPoolTaskExecutor-5执行异步任务:14线程ThreadPoolTaskExecutor-3执行异步任务:13线程ThreadPoolTaskExecutor-1执行异步任务:19线程ThreadPoolTaskExecutor-2执行异步任务:18线程ThreadPoolTaskExecutor-4执行异步任务:17

上一篇下一篇

猜你喜欢

热点阅读