I have several components with annotations @Scheduled, and I see that Spring only starts one at a time, even if they are scheduled to run at the same time.
My use case is as follows. I want each @Scheduled annotation to run in its thread, but only once for each thread.
Given this pseudocode with two schedulers:
@Scheduled(cron = "0 * * * * *")
public void methodA() {
log.info("Running method A");
executeLongRunningJob("Finished method A");
}
@Scheduled(cron = "0 * * * * *")
public void methodB() {
log.info("Running method B");
executeLongRunningJob("Finished method B");
}
private void executeLongRunningJob(String msg) {
Thread.sleep(70 seconds);
System.out.println(msg);
}
Note that the task takes longer than the scheduler has planned. It is important. I do not want the scheduler to start again before it is completed.
Running this code out of the box gives me this result:
Running method A
Finished method A
Running method B
Finished method B
Running method A
Finished method A
Running method B
Finished method B
... and so on
Thus, it is obvious that it runs both schedulers in the same thread.
@Async , , , .
Running method A
Running method B
Running method A
Running method B
Finished method A
Finished method B
... and so on
, :
Running method A
Running method B
Finished method A
Finished method B
Running method A
Running method B
Finished method A
Finished method B
... and so on
?
, , , , .
, , , .