threadpoolexecutor 不执行_ThreadPoolExecutor的线程调度及其中的问题

问题现象

在我们的系统中,使用了这样的配置来开启异步操作:
<task:annotation-driven executor="executor"    scheduler="scheduler" /><task:executor id="executor" pool-size="16-128"    keep-alive="60" rejection-policy="CALLER_RUNS"     queue-capacity="1000" />
客户端开启异步代码如下:
@Async()public Future calculateByLendId(int lendrequestId) {
        // 标记1    // 调用REST服务;监控调用时间。  }  // 获取Future后的处理如下try {
         keplerOverdue = summay4Overdue.get(5, TimeUnit.SECONDS);     // 后续处理} catch (Exception e) {
         // 标记2     // 异常报警 } 
然而在这种配置下,客户端在标记1处监控到的调用时间普遍在4s以内(平均时间不到1s,个别峰值请求会突破5s ,全天超过5s的请求不到10个。然而,在标记2处捕获到的超时异常却非常多,一天接近700+ 。 问题出在哪儿?

原因分析

上述问题相关代码的调用时序如下图所示。

ec829d3fab8c9af69a9e234f744db106.png

其中,rest client 与rest server间的交互时间可以明确监控到 ,用时超过5s的非常少。但是,get方法却经常抛出超时异常。经过初步分析,问题出现在ThreadPoolTaskExecutor的任务调度过程中。

任务调度逻辑

使用注解得到的bean是ThreadPoolTaskExecutor的实例。这个类本身并不做调度,而是将调度工作委托给了ThreadPoolExecutor。后者的任务调度代码如下:
/** * Executes the given task sometime in the future.  The task * may execute in a new thread or in an existing pooled thread. * * If the task cannot be submitted for execution, either because this * executor has been shutdown or because its capacity has been reached, * the task is handled by the current {
    @code RejectedExecutionHandler}. * * @param command the task to execute * @throws RejectedExecutionException at discretion of *         {
    @code RejectedExecutionHandler}, if the task *         cannot be accepted for execution * @throws NullPointerException if {
    @code command} is null */public void execute(Runnable command) {
        if (command == null)        throw new NullPointerException();    /*     * Proceed in 3 steps:     *     * 1. If fewer than corePoolSize threads are running, try to     * start a new thread with the given command as its first     * task.  The call to addWorker atomically checks runState and     * workerCount, and so prevents false alarms that would add     * threads when it shouldn't, by returning false.     *     * 2. If a task can be successfully queued, then we still need     * to double-ch
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值