春季-由相同的线程池支持TaskExecutor和TaskScheduler [英] Spring - Have TaskExecutor and TaskScheduler backed by the same thread pool

查看:183
本文介绍了春季-由相同的线程池支持TaskExecutor和TaskScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是TaskScheduler和TaskExecutor的bean,如下所示:

I am a bean for TaskScheduler and TaskExecutor as following:

@Bean
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler s = new ThreadPoolTaskScheduler();

    s.setThreadNamePrefix("Task-Scheduler-");
    s.setPoolSize(10);
    s.setRemoveOnCancelPolicy(true);

    return s;
}

@Bean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor e = new ThreadPoolTaskExecutor();

    e.setThreadNamePrefix("Task-Executor-");
    e.setMaxPoolSize(10);
    e.setCorePoolSize(10);

    return e;
}

是否可以在TaskExecutor和TaskScheduler之间共享基础线程池执行程序服务?现在,每个10个固定线程有两个池,但是我想有一个20个线程的池.

Is it possible to share the underlying thread pool executor service between the TaskExecutor and the TaskScheduler? Now I have twice a pool of each 10 fixed threads, but I would like to have one single pool of 20 threads.

这些池将用于@ Async,@ Scheduled和@Retry注释.

These pools will be used for @Async, @Scheduled and @Retry annotatons.

推荐答案

您不能使用这两个类来完成此操作,因为它们是依赖于内部池的实现.

You can't do this by using those two classes, because they are implementations that rely on an internal pool.

但是,您可以使用共享的ThreadPool实现自己的TaskExecutorTaskScheduler类.

However, you can implement your own TaskExecutor and TaskScheduler class that use a shared ThreadPool.

请注意,尽管有几个空闲线程不会对性能产生太大影响,所以除非您知道拥有两个池是主要的性能瓶颈,否则我不会浪费时间.

Note though that a few idle threads is not going to have much of an impact on performance, so unless you know that having two pools is a major performance bottleneck, I wouldn't waste my time.

这篇关于春季-由相同的线程池支持TaskExecutor和TaskScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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