spring @Scheduled带注释的方法是否在不同的线程上运行?

时间:2022-06-15 21:02:05

I have several methods annotated with @Scheduled(fixedDelay=10000).

我有几个用@Scheduled注释的方法(fixedDelay = 10000)。

In the application context, I have this annotation driven setup:

在应用程序上下文中,我有这个注释驱动设置:

<task:annotation-driven />

The problem is, sometimes some of the method execution gets delayed by seconds and even minutes.

问题是,有时一些方法执行会延迟几秒甚至几分钟。

Im assuming that even if a method takes a while to finish executing, the other methods would still execute. So I don't understand the delay.

我假设即使方法需要一段时间才能完成执行,其他方法仍然会执行。所以我不明白延迟。

Is there a way to maybe lessen or even remove the delay?

有没有办法可以减少甚至消除延迟?

4 个解决方案

#1


34  

The documentation about scheduling says:

有关安排的文档说:

If you do not provide a pool-size attribute, the default thread pool will only have a single thread.

如果未提供pool-size属性,则默认线程池将只有一个线程。

So if you have many scheduled tasks, you should configure the scheduler, as explained in the documentation, to have a pool with more threads, to make sure one long task doesn't delay all the other ones.

因此,如果您有许多计划任务,您应该按照文档中的说明配置调度程序,以使池具有更多线程,以确保一个长任务不会延迟所有其他任务。

#2


37  

For completeness, code below shows the simplest possible way to configure scheduler with java config:

为了完整起见,下面的代码显示了使用java config配置调度程序的最简单方法:

@Configuration
@EnableScheduling
public class SpringConfiguration {

    @Bean(destroyMethod = "shutdown")
    public Executor taskScheduler() {
        return Executors.newScheduledThreadPool(5);
    }
    ...

When more control is desired, a @Configuration class may implement SchedulingConfigurer.

当需要更多控制时,@ Configuration类可以实现SchedulingConfigurer。

#3


16  

A method annotated with @Scheduled is meant to be run separately, on a different thread at a moment in time.

使用@Scheduled注释的方法意味着在某个时刻在不同的线程上单独运行。

If you haven't provided a TaskScheduler in your configuration, Spring will use

如果您尚未在配置中提供TaskScheduler,Spring将使用

Executors.newSingleThreadScheduledExecutor();

which returns an ScheduledExecutorService that runs on a single thread. As such, if you have multiple @Scheduled methods, although they are scheduled, they each need to wait for the thread to complete executing the previous task. You might keep getting bigger and bigger delays as the the queue fills up faster than it empties out.

它返回在单个线程上运行的ScheduledExecutorService。因此,如果您有多个@Scheduled方法,虽然它们已被调度,但它们每个都需要等待线程完成执行上一个任务。随着队列填满的速度快于排空,您可能会继续变得越来越大。

Make sure you configure your scheduling environment with an appropriate amount of threads.

确保使用适当数量的线程配置调度环境。

#4


5  

you can use:

您可以使用:

@Bean()
public  ThreadPoolTaskScheduler  taskScheduler(){
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(2);
    return  taskScheduler;
}

#1


34  

The documentation about scheduling says:

有关安排的文档说:

If you do not provide a pool-size attribute, the default thread pool will only have a single thread.

如果未提供pool-size属性,则默认线程池将只有一个线程。

So if you have many scheduled tasks, you should configure the scheduler, as explained in the documentation, to have a pool with more threads, to make sure one long task doesn't delay all the other ones.

因此,如果您有许多计划任务,您应该按照文档中的说明配置调度程序,以使池具有更多线程,以确保一个长任务不会延迟所有其他任务。

#2


37  

For completeness, code below shows the simplest possible way to configure scheduler with java config:

为了完整起见,下面的代码显示了使用java config配置调度程序的最简单方法:

@Configuration
@EnableScheduling
public class SpringConfiguration {

    @Bean(destroyMethod = "shutdown")
    public Executor taskScheduler() {
        return Executors.newScheduledThreadPool(5);
    }
    ...

When more control is desired, a @Configuration class may implement SchedulingConfigurer.

当需要更多控制时,@ Configuration类可以实现SchedulingConfigurer。

#3


16  

A method annotated with @Scheduled is meant to be run separately, on a different thread at a moment in time.

使用@Scheduled注释的方法意味着在某个时刻在不同的线程上单独运行。

If you haven't provided a TaskScheduler in your configuration, Spring will use

如果您尚未在配置中提供TaskScheduler,Spring将使用

Executors.newSingleThreadScheduledExecutor();

which returns an ScheduledExecutorService that runs on a single thread. As such, if you have multiple @Scheduled methods, although they are scheduled, they each need to wait for the thread to complete executing the previous task. You might keep getting bigger and bigger delays as the the queue fills up faster than it empties out.

它返回在单个线程上运行的ScheduledExecutorService。因此,如果您有多个@Scheduled方法,虽然它们已被调度,但它们每个都需要等待线程完成执行上一个任务。随着队列填满的速度快于排空,您可能会继续变得越来越大。

Make sure you configure your scheduling environment with an appropriate amount of threads.

确保使用适当数量的线程配置调度环境。

#4


5  

you can use:

您可以使用:

@Bean()
public  ThreadPoolTaskScheduler  taskScheduler(){
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(2);
    return  taskScheduler;
}