详解Springboot对多线程的支持
作者:青春无罪 发布时间:2023-09-21 02:18:21
这两天看阿里的JAVA开发手册,到多线程的时候说永远不要用 new Thread()这种方式来使用多线程。确实是这样的,我一直在用线程池,到了springboot才发现他已经给我们提供了很方便的线程池机制。
本博客代码托管在github上https://github.com/gxz0422042...
一、介绍
Spring是通过任务执行器(TaskExecutor)来实现多线程和并发编程,使用ThreadPoolTaskExecutor来创建一个基于线城池的TaskExecutor。在使用线程池的大多数情况下都是异步非阻塞的。我们配置注解@EnableAsync可以开启异步任务。然后在实际执行的方法上配置注解@Async上声明是异步任务。
二、配置类
配置类代码如下:
package com.spartajet.springbootlearn.thread;
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;
/**
* @description
* @create 2017-02-22 下午11:53
* @email gxz04220427@163.com
*/
@Configuration
@EnableAsync
public class ThreadConfig implements AsyncConfigurer {
/**
* The {@link Executor} instance to be used when processing async
* method invocations.
*/
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(15);
executor.setQueueCapacity(25);
executor.initialize();
return executor;
}
/**
* The {@link AsyncUncaughtExceptionHandler} instance to be used
* when an exception is thrown during an asynchronous method execution
* with {@code void} return type.
*/
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
解读:
利用EnableAsync来开启Springboot对于异步任务的支持
配置类实现接口AsyncConfigurator,返回一个ThreadPoolTaskExecutor线程池对象。
三、任务执行
任务执行代码:
package com.spartajet.springbootlearn.thread;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @description
* @create 2017-02-23 上午12:00
* @email gxz04220427@163.com
*/
@Service
public class AsyncTaskService {
@Async
public void executeAsyncTask(int i) {
System.out.println("线程" + Thread.currentThread().getName() + " 执行异步任务:" + i);
}
}
代码解读:
通过@Async注解表明该方法是异步方法,如果注解在类上,那表明这个类里面的所有方法都是异步的。
四、测试代码
package com.spartajet.springbootlearn;
import com.spartajet.springbootlearn.thread.AsyncTaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith (SpringRunner.class)
@SpringBootTest
public class SpringbootLearnApplicationTests {
@Autowired
private AsyncTaskService asyncTaskService;
@Test
public void contextLoads() {
}
@Test
public void threadTest() {
for (int i = 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
总结
以上所述是小编给大家介绍的Springboot对多线程的支持网站的支持!
来源:https://segmentfault.com/a/1190000015766938


猜你喜欢
- 背景由于前前前阵子写了个壳,得去了解类的加载流程,当时记了一些潦草的笔记。这几天把这些东西简单梳理了一下,本文分析的代码基于Android8
- 问题描述利用选择排序把一列数组按从小到大或从大到小排序(一)、选择排序思想以从小到大为例:1、第一轮选择,从第一个数开始,依次比较后面所有的
- spring中事务处理原理 利用aop生成代理对象执行带有Transactional事务注解的
- PhotoView的简介:这是一个图片查看库,实现图片浏览功能,支持pinch(捏合)手势或者点击放大缩小。支持在ViewPager中翻页浏
- 最近接触到个新项目,发现它用了一个比较有意思的框架,可以说实现了我刚入行时候的梦想,所以这里马不停蹄的和大家分享下。在我刚开始工作接触的项目
- 由于我使用的是properties类型的配置文件,在对druid的参数进行配置的时候,多加了druid,也就是spring.datasour
- 问题说明:IDEA编译的时候乱码,Build Output提示信息乱码�����。解决方案一:将Help—>Edit Cusuom V
- 前言在单机应用时代,我们对一个共享的对象进行多线程访问的时候,使用java的synchronized关键字或者ReentrantLock类对
- 1.图集导航1.1 为什么对包名的命名要有所规范呢!使用规范的命名有益于程序的开发和后期阅读通俗的说:就是自己写的代码别人也能看的懂,代码结
- 在AndroidMenifest.xml中,常常会有下面的语句: <uses-sdk android:minSdkVersion=&q
- 当遇到以下场景:其他人写的单元测试影响统计结果一些需要调用外部接口的测试暂不运行需要在非本机环境上运行一些不回滚的单元测试则有必要选择以下方
- 先来简单说一下本文所要实现的功能:用户在浏览网页的时候,长按某一区域,识别如果是图片,则弹出弹框,出现保存图片的功能。同时识别图片是否是二维
- 本文实例为大家分享了java图形用户界面实现菜单功能的具体代码,供大家参考,具体内容如下题目:编写一个图形用户界面,实现菜单的功能。有3个一
- 本文实例讲述了C#获取USB事件API。分享给大家供大家参考。具体如下:const int WM_DEVICECHANGE = 0x2190
- 本文实例讲述了C#中的try catch finally用法。分享给大家供大家参考。具体分析如下:try中的程序块是有可能发生错误的程序块,
- import java.util.Scanner;public class VariableExchange { &n
- 什么是JSON?JSON (JavaScript Object Notation) is a lightweight data-interc
- 一、什么是 RestTemplate?RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK Ht
- 结构体概念在C#中,结构体是值类型,一般适用于表示类似Point、Rectangle、Color的对象值类型能够降低对堆的管理、使用。降低垃
- Java内部类(Inner Class),类似的概念在C++里也有,那就是嵌套类(Nested Class),乍看上去内部类似乎有些多余,它