Java分享客栈里,SpringBoot线程池参数配置难题,你有什么高招?
- 内容介绍
- 文章标签
- 相关推荐
本文共计985个文字,预计阅读时间需要4分钟。
一、前言——首先说一句,如果比较急迫的路点进来的,可以先收藏,有时间或用到的时候再看;
二、我相信很多人都会有一个困扰,这个困扰和我的以前一样,就是线程池这个有趣的小玩意儿。
一、前言首先说一句,如果比较忙顺路点进来的,可以先收藏,有时间或用到了再看也行;
我相信很多人会有一个困惑,这个困惑和我之前一样,就是线程池这个玩意儿,感觉很高大上,用起来很fashion,本地环境测试环境调试毫无问题,但是一上线就出问题。
然后百度一大堆资料,发现都在讲线程池要自定义,以及各种配置参数,看完之后点了点头原来如此,果断配置,结果线上还是出问题。
归根究底,还是对自定义线程池的配置参数不了解造成的,本篇就通过一个很简单的案例给大家梳理清楚线程池的配置,以及线上环境到底该如何配置。
二、案例 1、编写案例
自定义一个线程池,并加上初始配置。
核心线程数10,最大线程数50,队列大小200,自定义线程池名称前缀为my-executor-,以及线程池拒绝策略为AbortPolicy,也是默认策略,表示直接放弃任务。
package com.example.executor.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
@EnableScheduling
@Slf4j
public class AsyncConfiguration {
/**
* 自定义线程池
*/
@Bean(name = "myExecutor")
public Executor getNetHospitalMsgAsyncExecutor() {
log.info("Creating myExecutor Async Task Executor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("my-executor-");
// 拒绝策略:直接拒绝抛出异常
executor.setRejectedExecutionHandler(
new ThreadPoolExecutor.AbortPolicy());
return executor;
}
}
接下来,我们写一个异步服务,直接使用这个自定义线程池,并且模拟一个耗时5秒的发消息业务。
package com.example.executor.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* <p>
* 异步服务
* </p>
*
* @author 福隆苑居士,公众号:
* @since 2022/4/30 11:41
*/
@Service
@Slf4j
public class AsyncService {
/**
* 模拟耗时的发消息业务
*/
@Async("myExecutor")
public void sendMsg() throws InterruptedException {
log.info("[AsyncService][sendMsg]>>>> 发消息....");
TimeUnit.SECONDS.sleep(5);
}
}
然后,我们写一个TestService,使用Hutools自带的并发工具来调用上面的发消息服务,并发数设置为200,也就是同时开启200个线程来执行业务。
package com.example.executor.service;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* <p>
* 测试服务
* </p>
*
* @author 福隆苑居士,公众号:
* @since 2022/4/30 11:45
*/
@Service
@Slf4j
public class TestService {
private final AsyncService asyncService;
public TestService(AsyncService asyncService) {
this.asyncService = asyncService;
}
/**
* 模拟并发
*/
public void test() {
ConcurrencyTester tester = ThreadUtil.concurrencyTest(200, () -> {
// 测试的逻辑内容
try {
asyncService.sendMsg();
} catch (InterruptedException e) {
log.error("[TestService][test]>>>> 发生异常: ", e);
}
});
// 获取总的执行时间,单位毫秒
log.info("总耗时:{}", tester.getInterval() + " ms");
}
}
最后,写一个测试接口。
package com.example.executor.controller;
import com.example.executor.service.TestService;
import org.springframework.pan.baidu.com/doc/share/TES95Wnsy3ztUp_Al1L~LQ-567189327526315
提取码: 2jjy
本人原创文章纯手打,觉得有一滴滴用处的话就请点个推荐吧。
不定期分享实际工作中的经验和趣事,感兴趣的话就请关注一下吧~
喜欢就点一下推荐吧~~
本文共计985个文字,预计阅读时间需要4分钟。
一、前言——首先说一句,如果比较急迫的路点进来的,可以先收藏,有时间或用到的时候再看;
二、我相信很多人都会有一个困扰,这个困扰和我的以前一样,就是线程池这个有趣的小玩意儿。
一、前言首先说一句,如果比较忙顺路点进来的,可以先收藏,有时间或用到了再看也行;
我相信很多人会有一个困惑,这个困惑和我之前一样,就是线程池这个玩意儿,感觉很高大上,用起来很fashion,本地环境测试环境调试毫无问题,但是一上线就出问题。
然后百度一大堆资料,发现都在讲线程池要自定义,以及各种配置参数,看完之后点了点头原来如此,果断配置,结果线上还是出问题。
归根究底,还是对自定义线程池的配置参数不了解造成的,本篇就通过一个很简单的案例给大家梳理清楚线程池的配置,以及线上环境到底该如何配置。
二、案例 1、编写案例
自定义一个线程池,并加上初始配置。
核心线程数10,最大线程数50,队列大小200,自定义线程池名称前缀为my-executor-,以及线程池拒绝策略为AbortPolicy,也是默认策略,表示直接放弃任务。
package com.example.executor.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
@EnableScheduling
@Slf4j
public class AsyncConfiguration {
/**
* 自定义线程池
*/
@Bean(name = "myExecutor")
public Executor getNetHospitalMsgAsyncExecutor() {
log.info("Creating myExecutor Async Task Executor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(50);
executor.setQueueCapacity(200);
executor.setThreadNamePrefix("my-executor-");
// 拒绝策略:直接拒绝抛出异常
executor.setRejectedExecutionHandler(
new ThreadPoolExecutor.AbortPolicy());
return executor;
}
}
接下来,我们写一个异步服务,直接使用这个自定义线程池,并且模拟一个耗时5秒的发消息业务。
package com.example.executor.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* <p>
* 异步服务
* </p>
*
* @author 福隆苑居士,公众号:
* @since 2022/4/30 11:41
*/
@Service
@Slf4j
public class AsyncService {
/**
* 模拟耗时的发消息业务
*/
@Async("myExecutor")
public void sendMsg() throws InterruptedException {
log.info("[AsyncService][sendMsg]>>>> 发消息....");
TimeUnit.SECONDS.sleep(5);
}
}
然后,我们写一个TestService,使用Hutools自带的并发工具来调用上面的发消息服务,并发数设置为200,也就是同时开启200个线程来执行业务。
package com.example.executor.service;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* <p>
* 测试服务
* </p>
*
* @author 福隆苑居士,公众号:
* @since 2022/4/30 11:45
*/
@Service
@Slf4j
public class TestService {
private final AsyncService asyncService;
public TestService(AsyncService asyncService) {
this.asyncService = asyncService;
}
/**
* 模拟并发
*/
public void test() {
ConcurrencyTester tester = ThreadUtil.concurrencyTest(200, () -> {
// 测试的逻辑内容
try {
asyncService.sendMsg();
} catch (InterruptedException e) {
log.error("[TestService][test]>>>> 发生异常: ", e);
}
});
// 获取总的执行时间,单位毫秒
log.info("总耗时:{}", tester.getInterval() + " ms");
}
}
最后,写一个测试接口。
package com.example.executor.controller;
import com.example.executor.service.TestService;
import org.springframework.pan.baidu.com/doc/share/TES95Wnsy3ztUp_Al1L~LQ-567189327526315
提取码: 2jjy
本人原创文章纯手打,觉得有一滴滴用处的话就请点个推荐吧。
不定期分享实际工作中的经验和趣事,感兴趣的话就请关注一下吧~
喜欢就点一下推荐吧~~

