简体   繁体   中英

How does Spring's @Scheduled annotation work?

In my app I have 3 scheduled methods in 3 separate classes:

@Component
public class ClassA {
@Scheduled(fixedDelay = 5000L)
    public void methodA(){
        //do task "A"
    }
}

@Component
public class ClassB {
@Scheduled(fixedDelay = 5000L)
    public void methodB(){
        //do task "B"
    }
}

@Component
public class ClassF {
@Scheduled(fixedDelay = 5000L)
    public void methodF(){
        //do task "F"
    }
}

In apps applicationContext there are such lines:

<task:annotation-driven executor="myExecutor" scheduler="scheduler"/>
<task:executor id="myExecutor"/>
<task:scheduler id="scheduler"/>

I can't figure it out from the log files, so here are my questions:

  • How do these methods executes? In parallel or successively? If successively then how can it be reconfigured to work in parallel?
  • Is this confuguration multithreaded (for each method) or not (and if not - again, how can it be changed)?

It uses the java Executor api in background. Everything you need to know can be found in the documentation

You basically choose a TaskExecutor implementation and then set how many processing threads you want.

In your example the 3 methods will be executed continuously with a fixed delay of 5 seconds. But if you define only 1 processing thread, and one method takes a long time, then the execution of the other 2 methods is postponed. If you don't want to be dependent of the other functions being executed with the @Scheduled, you would need 3 processing threads in this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM