调整Spring容器的任务支持使用的线程
是否可以调整Spring容器的任务支持使用的线程数?
Is it possible to adjust the number of threads used by the Spring container's task support?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在运行时调整它,那么我相信您唯一的选择是以编程方式进行。
将
java.util.concurrent.ExecutorService
注入到 Spring 的任务支持中,如下所示:然后您可以像这样调整
ExecutorService
使用的线程:我已经写了更多关于并发的文章Spring 这里。
If you want to adjust it at runtime, then I believe your only choice is to do it programatically.
Inject a
java.util.concurrent.ExecutorService
into Spring's task support like this:Then you can adjust the threads used by the
ExecutorService
like this:I have written more about concurrency with Spring here.
如果你的意思是任务,就像计划任务一样,那么我不相信,至少不直接相信,因为任务执行通常是同步的 - 也就是说,同一任务的两次调用在同一个线程上执行 - 这至少是对于 JDK
Timer
类为 true,该类为每个 Timer 使用专用线程 - 因此计时器的多次触发在同一线程上执行。如果您希望针对线程池执行任务,最好的办法是将任务与计时器分开,并让计时器在线程池上调用您的任务。您可以配置运行任务的线程池,这样您就可以完全控制线程数量。
计时器调用的任务
在您的 spring 配置中如下所示
If you mean tasks, as in scheduled tasks, then I don't believe so, at least not directly, since task execution is normally synchronous - that is, two invocations of the same task are executed on the same thread - this is at least true for the JDK
Timer
class which uses a dedicated thread for each Timer - so multiple firings of the timer execute on the same thread.If you want to have tasks executed against a thread pool, your best bet is to separate the task from the timer, and have the timer invoke your task on a thread pool. You get to configure the thread pool running the task and so you have full control over the number of threads.
The task invoked by the Timer looks like this
In your spring config