尽管线程可用,但任务被拒绝 [英] Task getting rejected although threads are available

查看:370
本文介绍了尽管线程可用,但任务被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个奇怪的情况,我有一个线程池,其中的maxPoolSize为100,队列大小为30,corePoolSize为50.在重新启动应用程序一段时间后,我可以在日志中看到RejectedExecution错误,该错误指出即使线程是可用,任务被拒绝.

I am facing a strange situation where I have a thread pool with maxPoolSize as 100 and queue size as 30 and corePoolSize as 50. After application is restarted in some time I can see RejectedExecution errors in logs which state that even though threads are available , task was rejected.

org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@593a81ea[Running, pool size = 100, active threads = 3, queued tasks = 17, completed tasks = 57463]]

我多次进行线程转储,发现该池的线程正在执行Timed_Waiting.上面的日志是正确的,每个转储也只能运行3个,但是我不明白为什么其余97个线程对系统不可用.以下是97个线程共有的线程转储:

I took thread dump many times and found that threads of this pool are doing Timed_Waiting. Above log is correct that only 3 are runnable as per dump also but I am not able to understand why rest of the 97 threads are not available to the system. Below is the thread dump which is common for 97 threads :

YYYThreadPoolExecutor-100" #6414 prio=5 os_prio=0 tid=0x00007f0830dce800 nid=0x3a39 waiting on condition [0x00007f064b6b5000]
   java.lang.Thread.State: TIMED_WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <0x00007f0a32b21088> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)

系统是具有100 GB RAM的16核盒子.对于16个内核,当前的CPU利用率仅为103%,RAM利用率小于15GB.

System is a 16-core box with 100 GB RAM. Current cpu utilisation is just 103% for 16 cores and RAM utilisation is <15GB.

即使我也将线程池大小增加到500,同样的情况也会延迟出现.

Even if I increase thread pool size to 500 also , same situation arrives with a delay.

以前有人遇到过这种情况吗? 预先感谢.

Has anybody faced this situation before? Thanks in advance.

推荐答案

请参考有关

使用给定的初始参数和默认线程工厂创建一个新的ThreadPoolExecutor.

Creates a new ThreadPoolExecutor with the given initial parameters and default thread factory.

参数:

corePoolSize-保留在池中的线​​程数,即使它们处于空闲状态,除非设置了allowCoreThreadTimeOut

corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set

maximumPoolSize-池中允许的最大线程数

maximumPoolSize - the maximum number of threads to allow in the pool

keepAliveTime-当线程数大于内核数时,这是多余的空闲线程将在终止之前等待新任务的最长时间.

keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

unit-keepAliveTime参数的时间单位

unit - the time unit for the keepAliveTime argument

workQueue-用于在执行任务之前保留任务的队列.此队列将仅保存由execute方法提交的Runnable任务.

workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.

handler-由于达到线程边界和队列容量而被阻塞执行时使用的处理程序

根据提供的详细信息,由于数量巨大,您尚未达到线程池范围,但可能已达到队列容量.

From the details you have provided, you have not reached Thread pool bounds due to huge number but you might have reached queue capacity.

您不需要大量线程.相反,您可以增加队列容量.

You don't need high number for number of threads. Instead you can increase queue capacity.

通常将队列大小配置为更高的值,以数字线程池的大小为准. maximumPoolSize可以配置为计算机中CPU核心的数量:Runtime.getRuntime().availableProcessors()

Generally queue size will be configured higher value to number thread pool size. maximumPoolSize can be configured to number of CPU cores in your machine : Runtime.getRuntime().availableProcessors()

这篇关于尽管线程可用,但任务被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆