Spring 使用 ThreadPoolTask​​Executor 的默认队列大小是多少? [英] What's Spring's default queue size with a ThreadPoolTaskExecutor?

查看:18
本文介绍了Spring 使用 ThreadPoolTask​​Executor 的默认队列大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 7 中使用 Spring 4.3.8.RELEASE.我想创建一个线程池来执行任务,所以我在我的 Spring contxet 中设置了以下内容

I'm using Spring 4.3.8.RELEASE with Java 7. I want to create a thread pool to execute tasks so I have set up the following in my Spring contxet

<bean id="myThreadFactory" class="org.springframework.scheduling.concurrent.CustomizableThreadFactory">
    <constructor-arg value="mythread-"/>
</bean>
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="threadFactory" ref="myThreadFactory"/>
    <property name="corePoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
</bean>

为了不破坏机器 CPU 使用率,我想限制系统中可以存在的并发线程的数量(我假设这就是 maxPoolSize 所做的).但我不希望任务被丢弃.如果我向我的 taskPoolExecutor 添加超过 50 个任务,那么第 51 个会发生什么?更重要的是,在开始删除之前可以添加的默认任务数量是多少?

So as not to crush machine CPU usage, I want to limit the amount of concurrent threads that can exist in the system (I assume that's what maxPOolSize does). But I don't want tasks to get dropped. If I add more than 50 tasks to my taskPoolExecutor, what happens to number 51? More importantly, what is the default number of tasks that can be added before they start getting dropped?

推荐答案

设置 maxPoolSize 隐式允许任务被丢弃.但是,默认队列容量是 Integer.MAX_VALUE,实际上是无穷大.

Setting maxPoolSize implicitly allows for tasks to get dropped. However, the default queue capacity is Integer.MAX_VALUE, which, for practical purposes, is infinity.

需要注意的是 ThreadPoolTask​​Executor 在下面使用了一个 ThreadPoolExecutor,它有一种不寻常的排队方法,在 文档:

Something to watch out for is that ThreadPoolTaskExecutor uses a ThreadPoolExecutor underneath, which has a somewhat unusual approach to queueing, described in the docs:

如果 corePoolSize 或更多线程正在运行,则 Executor 总是更喜欢排队请求而不是添加新线程.

If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.

这意味着 maxPoolSize 仅在队列已满时才相关,否则线程数永远不会超过 corePoolSize.例如,如果我们将从未完成的任务提交到线程池:

This means that maxPoolSize is only relevant when the queue is full, otherwise the number of threads will never grow beyond corePoolSize. As an example, if we submit tasks that never complete to the thread pool:

  • 第一个 corePoolSize 提交将启动一个新线程;
  • 之后,所有提交进入队列;
  • 如果队列有限且容量耗尽,则每次提交都会启动一个新线程,直到池中有maxPoolSize个线程;
  • 当池和队列都已满时,新的提交将被拒绝.
  • the first corePoolSize submissions will start a new thread each;
  • after that, all submissions go to the queue;
  • if the queue is finite and its capacity is exhausted, each submission starts a new thread, until there are maxPoolSize threads in the pool;
  • when both the pool and the queue are full, new submissions are rejected.

这篇关于Spring 使用 ThreadPoolTask​​Executor 的默认队列大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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