简体   繁体   中英

@Scheduled and scheduledExecutorService.scheduleWithFixedRate in java

What is the difference between @Scheduled annotation in Spring and using ScheduledExecutorService's scheduleWithFixedDelay method in Springboot? With the help of code, can someone demonstrate the exact usage difference and when to opt for which one?

Please have a look at https://www.baeldung.com/spring-task-scheduler . The @Scheduled is a generic implementation, which you can extend and configure for your needs. While the scheduleWithFixedDelay is a specific implementation that you can directly use. Ideally, it is fine to use either of them, but if your functionality is not supposed to change, it is better to use the implementation (I mean scheduleWithFixedDelay )

@Scheduled without a cron expression, fixeddelay or fixedrate is nothing. How can it know the interval, if not specified?

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

fixedDelay ensures that there is always n seconds duration

if first invocation is at 9:00:00 and fixedDelay = 10minutes, process takes 15 seconds to complete, then next cycle start = 09:10:15

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