Spring @Scheduled timings, you really understand Well?

As we all know, @ Scheduled timing of a spring annotation, so the timing is so simple.

 

Just mean a few things here ↓

 

Problem description:

This timer is divided into three types ↓

(1) cron point execution

(2) fixedRate how often to perform

(3) on fixedDelay time how long after the task execution

The above concept is easy to understand, popular and easy to remember; but a few need to understand the following scenarios:

 - When using cron, if set to every 1min once, when the last task execution is not completed within one minute, then the next task will block would be executed? If you will perform when executed, immediate implementation?

 - Similarly, when using fixedRate, if the interval is set to 1s executed once, not one second a task executed, whether the next will block the next execution is at what time?

 - When multiple tasks simultaneously using cron, executed between them on other time-consuming tasks influential thing?

 - three modes mix multiple tasks, have an impact on other time-consuming tasks between them thing?

 

Answer 2 Answer:

1,2 figure out the problem just look at this picture below - three kinds of timer task execution cycle diagram:

 

 The explanations of the source, this figure borrowed from  https://www.cnblogs.com/zouhong/p/11332126.html

 

The answer questions 3,4:

  A plurality of multi-task or tasks cron mixing using three timing type, will always influence each other between them; eg: performing a task, another task even to the execution time to wait before re-executing the task execution, because @Scheduled regular tasks are single-threaded, screenshots as evidence:

 

 So if you do not affect each other between multiple tasks, you can increase the number of threads, the exact number depending on the number of tasks may be, Spring 1.5.8 configuration code is as follows (other versions may differ):

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

Guess you like

Origin www.cnblogs.com/lzj123/p/12024668.html