【问题标题】:TaskRejectedException throw and thread pool is not fullTaskRejectedException 抛出和线程池未满
【发布时间】:2019-11-06 19:48:01
【问题描述】:

我使用 spring 线程池来管理我的项目中的线程。 但是当我的代码运行时出现了一些问题。 我得到如下异常:

org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@4cfc01ab[Running, pool size = 200, active threads = 0, queued tasks = 40, completed tasks = 7990]] did not accept task: java.util.concurrent.FutureTask@6ba9fcd5

当池“活动线程”为零时,它会抛出 TaskRejectedException

我已经阅读了 Spring 的文档和源代码,但一无所获。

我的 TaskExecutor 类:

@Bean
public ThreadPoolTaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setKeepAliveSeconds(200);
    taskExecutor.setCorePoolSize(200);
    taskExecutor.setMaxPoolSize(200);
    taskExecutor.setQueueCapacity(40);
    taskExecutor.setRejectedExecutionHandler(new 
    ThreadPoolExecutor.AbortPolicy());
    return taskExecutor;
}

active threads 有时是 0 有时是 10 ,从来不是 200 ,很奇怪。

【问题讨论】:

  • 任何解决方案????

标签: java spring threadpool threadpoolexecutor


【解决方案1】:

在异常消息中它具有“排队任务 = 40”。由于在您的设置中您有 setQueueCapacity(40),因此当排队任务已经达到 40 时执行线程将引发该异常。

如果您提交任务的速度非常快,我会尝试增加队列容量。线程不会立即从队列移动到活动状态,尤其是在 JVM 繁忙时。

就活动线程数而言,它只是一个近似值。 ThreadPoolExecutor.getActiveThreadCount 的 Java 文档:

/**
 * Returns the approximate number of threads that are actively
 * executing tasks.
 *
 * @return the number of threads
 */
public int getActiveCount() {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2011-11-09
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多