ThreadPoolTaskExecutor报TaskRejectedException异常

ThreadPoolTaskExecutor异常

先贴一下异常的源码

public void execute(Runnable task) {
    ThreadPoolExecutor executor = this.getThreadPoolExecutor();

    try {
        executor.execute(task);
    } catch (RejectedExecutionException var4) {
        throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, var4);
    }
}

execute提示,如果达到线程承载能力或者不能提交该任务,就提示该异常

/**
 * 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
 */

大概率就是线程池满了,队列已经塞爆

回过头看一下日志,pool size=500, active threads = 190(-_-!线程池的设置后面再记录吧)

原因:线程池设置不合理,同时代码没有做保护,在spring返回错误的同时,依然在往线程池里面丢

修改如下

while (true) {
    try {
        syncShardUseExecutor.execute(() -> xxxx);
        break;
    } catch (RejectedExecutionException e) {
        try {
            sleepCount++;
            Thread.sleep(1000);
        } catch (InterruptedException ite) {
            log.error("任务执行sleep错误:",ite);
        }

    } finally {
        if (sleepCount > 10) {
            log.error("任务执行" + sleepCount + "仍然失败,抛弃");
            break;
        }
    }
}

你可能感兴趣的:(Spring,Java)