Does spring @Scheduled annotated methods runs on different threads?

前端 未结 8 2324
时光取名叫无心
时光取名叫无心 2020-11-30 01:19

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

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

<         


        
8条回答
  •  渐次进展
    2020-11-30 01:32

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

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

    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.

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

提交回复
热议问题