记一次ThreadPoolTaskExecutor的坑

2023-12-05 14:28

本文主要是介绍记一次ThreadPoolTaskExecutor的坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

起因:

开发环境一切正常
部署到UAT环境后,项目中使用@Async修饰的方法没有执行。

临时解决方法:

先去掉该注解改成同步执行。

问题排查过程:

1.创建一个测试controller,用于观察线程池情况

package org.example.controller;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.example.service.MyTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Map;@RequestMapping(value = "/test")
@RestController
public class MyTestController {@Autowiredprivate ApplicationContext applicationContext;@Autowiredprivate MyTestService myTestService;@GetMapping({"/threadPools"})public JSONObject threadPools(){Map<String, ThreadPoolTaskExecutor> threadMap = applicationContext.getBeansOfType(ThreadPoolTaskExecutor.class);String json = JSON.toJSONString(threadMap);JSONObject jsonObject = JSON.parseObject(json);return jsonObject;}@GetMapping(value = "/test1")public String test1(){myTestService.test1();return "ok";}}
package org.example.service;import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Slf4j
@Service
public class MyTestService {@Async//("taskExecutor")public void test1(){log.info("test1");}}

浏览器方法该接口:http://localhost:8080/test/threadPools

{"taskExecutor": {"activeCount": 3,"threadNamePrefix": "taskExecutor-","poolSize": 3,"threadPoolExecutor": {"activeCount": 3,"threadFactory": {"$ref": "$.taskExecutor"},"largestPoolSize": 3,"poolSize": 3,"taskCount": 3,"rejectedExecutionHandler": {},"corePoolSize": 3,"completedTaskCount": 0,"terminating": false,"maximumPoolSize": 5,"queue": [],"shutdown": false,"terminated": false},"corePoolSize": 3,"threadPriority": 5,"maxPoolSize": 5,"keepAliveSeconds": 60,"daemon": false}
}

再调用test1接口把普通任务提交到该线程池:http://localhost:8080/test/test1
再观察线程池情况:

{"taskExecutor": {"activeCount": 3,"threadNamePrefix": "taskExecutor-","poolSize": 3,"threadPoolExecutor": {"activeCount": 3,"threadFactory": {"$ref": "$.taskExecutor"},"largestPoolSize": 3,"poolSize": 3,"taskCount": 5,"rejectedExecutionHandler": {},"corePoolSize": 3,"completedTaskCount": 0,"terminating": false,"maximumPoolSize": 5,"queue": [{"cancelled": false,"done": false},{"cancelled": false,"done": false}],"shutdown": false,"terminated": false},"corePoolSize": 3,"threadPriority": 5,"maxPoolSize": 5,"keepAliveSeconds": 60,"daemon": false}
}

发现等待队列queue节点多了几个,且activeCount一直保持3,而corePoolSize刚好也是3。
此时有理由怀疑,有3个任务没有结束,导致新的任务只能放在等待队列,因此没有执行新任务。

通过本地debug发现,系统启动后,公司的框架代码会往默认线程池里提交3个任务,而这3个任务都是while(true)循环。

package org.example.event;import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.context.ApplicationEvent;
@Getter
@Setter
@ToString
public class MyEvent  extends ApplicationEvent {public MyEvent(Object source) {super(source);}
}
package org.example.init;import org.example.event.MyEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;@Component
public class Initor {@Autowiredprivate ApplicationContext applicationContext;@PostConstructprivate void init(){applicationContext.publishEvent(new MyEvent("这是自定义事件1"));applicationContext.publishEvent(new MyEvent("这是自定义事件2"));applicationContext.publishEvent(new MyEvent("这是自定义事件3"));}
}
package org.example.listener;import lombok.extern.slf4j.Slf4j;
import org.example.event.MyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Slf4j
@Service
public class MyListener implements ApplicationListener<MyEvent>{@Async//("taskExecutor")@Overridepublic void onApplicationEvent(MyEvent myEvent) {log.info("myEvent:{}",myEvent);log.info("开始死循环");//3个死循环进入默认线程池,而默认线程池核心线程数是3,后续加入的任务都只能放在等待队列,永远没机会执行。while (true){try {Thread.sleep(10000);log.info("myEvent");} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

通过该线程池名称,搜索到该线程池的配置类:

package org.example.config;import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;@Slf4j
@Configuration
public class ThreadPoolConfig {@Bean//("taskExecutor")public Executor taskExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//核心线程数executor.setCorePoolSize(3);
//        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());//最大线程数executor.setMaxPoolSize(5);//队列容量executor.setQueueCapacity(1000);//线程活跃时间(秒)executor.setKeepAliveSeconds(60);//默认线程名称executor.setThreadNamePrefix("taskExecutor-");//拒绝策略executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//等待所有任务结束后再关闭线程池executor.setWaitForTasksToCompleteOnShutdown(true);
//        executor.initialize();不需要这句代码,因为ThreadPoolTaskExecutor实现了InitializingBean接口,其afterPropertiesSet方法会调用initialize()。return executor;}}

设置线程池的核心线程数使用了系统核心数,在UAT环境刚好是3,而其他环境大于3,这就是到了UAT环境突然有问题的原因了。

通过深入debug,发现spring获取默认线程池,是通过beanName为“taskExecutor”来查找的,而上面自定义线程池, @Bean注解没有指定名称,则取方法名“taskExecutor”,而该名称,碰巧是spring默认线程池的名称,导致该自定义线程池覆盖了spring的默认线程池,从而使用@Async(没有指定名称)都用的该线程池。

最终解决方案可以有2个:

1.把该自定义线程池换个名字,不要跟spring默认线程池名称一样。
2.把该自定义线程池的核心线程数改大点,起码要超过3个。

这篇关于记一次ThreadPoolTaskExecutor的坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/457933

相关文章

(function() {})();只执行一次

测试例子: var xx = (function() {     (function() { alert(9) })(); alert(10)     return "yyyy";  })(); 调用: alert(xx); 在调用的时候,你会发现只弹出"yyyy"信息,并不见弹出"10"的信息!这也就是说,这个匿名函数只在立即调用的时候执行一次,这时它已经赋予了给xx变量,也就是只是

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位

flume系列之:记录一次flume agent进程被异常oom kill -9的原因定位 一、背景二、定位问题三、解决方法 一、背景 flume系列之:定位flume没有关闭某个时间点生成的tmp文件的原因,并制定解决方案在博主上面这篇文章的基础上,在机器内存、cpu资源、flume agent资源都足够的情况下,flume agent又出现了tmp文件无法关闭的情况 二、

jmeter之仅一次控制器

仅一次控制器作用: 不管线程组设置多少次循环,它下面的组件都只会执行一次 Tips:很多情况下需要登录才能访问其他接口,比如:商品列表、添加商品到购物车、购物车列表等,在多场景下,登录只需要1次,我们期望的是重复执行登陆后面的接口来做压测,这就和事务相关,例如 事务1: 登录—>添加购物车 事务2: 登录—>购物车列表 事务3: 登录—>商品列表—>添加购物车 … 一、仅一次控制器案例 在

一次生产环境大量CLOSE_WAIT导致服务无法访问的定位过程

1.症状 生产环境的一个服务突然无法访问,服务的交互过程如下所示: 所有的请求都是通过网关进入,之后分发到后端服务。 现在的情况是用户服务无法访问商旅服务,网关有大量java.net.SocketTimeoutException: Read timed out报错日志,商旅服务也不断有日志打印,大多是回调和定时任务日志,所以故障点在网关和商旅服务,大概率是商旅服务无法访问导致网关超时。 后

关于一次速度优化的往事

来自:hfghfghfg, 时间:2003-11-13 16:32, ID:2292221你最初的代码 Button1 34540毫秒 5638毫秒  Button2 我的代码 这个不是重点,重点是这个  来自:hfghfghfg, 时间:2003-11-13 16:54, ID:22923085528毫秒 不会吧,我是赛杨1.1G  128M内存  w2000, delphi6  128M

一次关于生产环境服务无故宕机的排查过程

故事的开始 这个故事是在一年之前,当时我们的系统运行在客户的k8s环境上。然后很神奇的是每个月底我们都会服务宕机,当然我们开启了多个实例。当时的容器线条就像心跳图一样(或许有些描述的不太准确,我没有找到当时那个像心电图一样的容器资源监控图)。 第一次的排查 当时我们还是很有信心去解决这个问题的。由于每个月的月底都是业务使用的高峰时段,也就是说,从表象上来看,qps一高,容器就挂。 业务日

记一次knife4j文档请求异常 SyntaxError: Unexpected token ‘<‘, ... is not valid JSON

knife4j页面报错问题定位 前几天开发新接口,开发完成后想使用knife4j测试一下接口功能,突然发现访问页面报错提示:knife4j文档请求异常,但之前运行还是正常的,想想会不会与升级依赖有关系,启动其他微服务发现文档接口访问正常,排除因依赖版本升级导致在线API文档无法使用情况,还是和本服务新增接口有关系。 定位问题 首先f12打开调试台,重新刷新页面,看到console有报错提示

记一次项目启动报错问题

今天遇到了一个问题,困扰了我几个小时,虽然最后是一个小问题导致的。记录下,也算一个解决问题的方法。   前提:调用webservice时引用 <dependency><groupId>org.codehaus.xfire</groupId><artifactId>xfire-all</artifactId><version>1.2.6</version></dependency>

Java-IDEA模拟一个Redis服务器,与Redis客户端进行一次简单的交互。默认端口号:6379

首先要了解Redis的交互协议。 摘抄: 简单字符串(Simple Strings): 以 “+” 开头,例如 “+OK\r\n” 表示一个成功的响应。错误(Errors): 以 “-” 开头,例如 “-ERR unknown command\r\n” 表示一个错误响应。整数(Integers): 以 “:” 开头,例如 “:1000\r\n” 表示整数1000。批量字符串(Bulk St

记一次小程序开发过程

写在前面 前后两天花了大约四五个小时制作完了自己第一个小程序,当然是没法发布的,小程序的发布要求还是挺严格的:企业资质、HTTPS、审核。 先大概介绍下自己,我9年前和很多网友一样开始自学编程,这些年来什么语言都学过、什么平台都接触过,自己也做过十来个产品,所以编程基础不是很稳固但是各方面都相对比较熟悉,因此在接触小程序的时候上手比较快。 至于为什么现在选择开发小程序,原因很简单,尝尝鲜!