如何等待@Scheduled 直到上一个任务未完成?

Ree*_*ema 2 java spring spring-mvc spring-boot

我想哭我的调度程序直到我的任务完成。如果有时间执行第二个调度程序,它必须等到上一个任务未完成。我在 java Boot 应用程序中使用 @Schedule 。我想每 5 分钟将数据插入到数据库中,但我想保持我的日程安排,直到插入数据不完整仍然有时间进行第二次执行。 演示代码

@Scheduled(fixedRate = 2000)
    public void scheduleTaskWithFixedRate() {
        logger.info("Fixed Rate Task :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now()) );
    }
Run Code Online (Sandbox Code Playgroud)

MyT*_*nts 13

fixedDelay

fixedRate :使 Spring 定期运行任务,即使最后一次调用可能仍在运行。

fixedDelay :具体控制上次执行完成后的下一次执行时间。在代码中:

@Scheduled(fixedDelay=5000)
public void updateEmployeeInventory(){

}    

@Scheduled(fixedRate=5000)
public void updateEmployeeInventory(){

}
Run Code Online (Sandbox Code Playgroud)