Synchronousqueue-尚未达到最大池大小时被拒绝的任务被拒绝
我有一个代码,该代码旨在执行任务(1000+)。我的核心池大小为10,最大尺寸为100。我使用的是syncq,因此没有队列大小。
BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
ThreadPoolExecutor pool = new ThreadPoolExecutor(10,100, Long.MAX_VALUE, TimeUnit.NANOSECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
对于大约1000个请求,这可以正常运行,之后我开始获得TaskeDrejectedException。例外堆栈跟踪是像下面的 -
org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@56672127[Running, pool size = 20, active threads = 20, queue tasks = 1000, completed tasks = 13431]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$1@462b281
我试图调试问题并解决问题。
我期望发生的是:
- ThreadPoolExecutor有10个线程可以接受请求。
- 当超过10个任务进来并且尚未完成前10个任务时,将创建新线程,直到
- 100个任务运行时达到100(最大池大小),将发送101个任务,将发送到主线程(Callerrunspolicy)和阻止其他请求,直到可以运行空间为止。
发生了什么(根据日志):
- ThreadPooleExecutor从10个线程开始,最多可达20(而不是最大?)
- 队列大小显示为1000,当Syncq没有任何存储任务
- 任务的能力时,只要我们有1000个请求就会被拒绝处理。
完成的任务计数不是来自一次运行,它会随着时间的推移而产生的,因为其他时间在其他时间进行测试,而不是在我的测试时进行测试。
我不明白为什么池尺寸停止20,队列大小显示为1000(而不是0),以及为什么在我们与CallerSrunpolicy的Syncq时,任务被拒绝。有人知道Syncq或ThreadPoolExecutor出了什么问题吗?
如果是问题,我确实可以选择删除Syncq并添加一个有限的队列,但是此时,我不确定是什么原因导致了问题,因为我看到为Syncq打印了队列大小。
I have a piece of code that is meant to execute tasks (1000+). I have a core pool size of 10 and a max size of 100. I am using a SyncQ and so there is no queue size.
BlockingQueue<Runnable> queue = new SynchronousQueue<Runnable>();
ThreadPoolExecutor pool = new ThreadPoolExecutor(10,100, Long.MAX_VALUE, TimeUnit.NANOSECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
This runs fine for about 1000 requests after which I started getting TaskRejectedException. The exception stack trace is something like the below-
org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@56672127[Running, pool size = 20, active threads = 20, queue tasks = 1000, completed tasks = 13431]] did not accept task: org.springframework.aop.interceptor.AsyncExecutionInterceptor$1@462b281
I am trying to debug the problem and get it fixed.
What I expected to happen was:
- ThreadPoolExecutor has 10 threads to accept requests.
- When more than 10 tasks come in and previous 10 are not done yet, then new threads would be created until 100 (max pool size) is reached
- When 100 tasks are running, the 101st task would be sent to the main thread (CallerRunsPolicy) and blocks the other requests until space is available for them to run.
What is happening (as per logs):
- ThreadPoolExecutor starts with 10 threads and goes up to 20 (not the max?)
- Queue size shows as 1000 when SyncQ does not have any capacity to store tasks
- Tasks get rejected whenever we have >1000 requests to process.
The completed task count is not from a single run, it accrues over time because several others test at other times and not at the time of my testing.
I do not understand why the pool size stops at 20, queue size shows up as 1000 (instead of 0) and why tasks are getting rejected while we have syncQ with callersRunPolicy. Anyone knows what is going wrong with the SyncQ or the ThreadPoolExecutor?
I do have the option to remove SyncQ and add a bounded queue if that is the issue, but at this point I am not sure what is causing the issue as I see queue size being printed for a syncQ.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论