英文:
Spring @Scheduled annotation execute in order for the same time instance
问题
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
public class Scheduler {
@Scheduled(cron = "0 */1 * * * ?")
public void method1() {
System.out.println("1");
}
@Scheduled(cron = "0 */2 * * * ?")
public void method2(){
System.out.println("2");
}
@Scheduled(cron = "0 */1 * * * ?")
public void method3() {
System.out.println("3");
}
@Scheduled(cron = "0 */2 * * * ?")
public void method4() {
System.out.println("4");
}
}
英文:
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
public class Scheduler {
@Scheduled(cron = "0 */1 * * * ?")
public void method1() {
System.out.println("1");
}
@Scheduled(cron = "0 */2 * * * ?")
public void method2(){
System.out.println("2");
}
@Scheduled(cron = "0 */1 * * * ?")
public void method3() {
System.out.println("3");
}
@Scheduled(cron = "0 */2 * * * ?")
public void method4() {
System.out.println("4");
}
}
Actual Output:
1
3
1
3
2
4
3
1
1
2
3
4
1
3
3
1
2
4
The output that I am receiving is completely random on the same instance of time. But I want to order output for the same instance of time in the following way mentioned below:
1
3
1
2
3
4
1
3
1
2
3
4
Is this possible to achieve the above scenario using the same Cron Expression?
答案1
得分: 2
您需要添加更小的时间单位:
@Scheduled(cron = "0 */1 * * * ?")
public void method1() {
System.out.println("1");
}
@Scheduled(cron = "1 */2 * * * ?")
public void method2(){
System.out.println("2");
}
@Scheduled(cron = "2 */1 * * * ?")
public void method3() {
System.out.println("3");
}
@Scheduled(cron = "3 */2 * * * ?")
public void method4() {
System.out.println("4");
}
编辑:其他实现:
public void method1() {
System.out.println("1");
}
public void method2(){
System.out.println("2");
}
public void method3() {
System.out.println("3");
}
public void method4() {
System.out.println("4");
}
@Scheduled(cron = "0 */2 * * * *")
public void even() {
method1();
method2();
method3();
method4();
}
@Scheduled(cron = "0 1/2 * * * *")
public void odd() {
method1();
method3();
}
英文:
You would have to add smaller unit of time:
@Scheduled(cron = "0 */1 * * * ?")
public void method1() {
System.out.println("1");
}
@Scheduled(cron = "1 */2 * * * ?")
public void method2(){
System.out.println("2");
}
@Scheduled(cron = "2 */1 * * * ?")
public void method3() {
System.out.println("3");
}
@Scheduled(cron = "3 */2 * * * ?")
public void method4() {
System.out.println("4");
}
Edit: other implementation:
public void method1() {
System.out.println("1");
}
public void method2(){
System.out.println("2");
}
public void method3() {
System.out.println("3");
}
public void method4() {
System.out.println("4");
}
@Scheduled(cron = "0 */2 * * * *")
public void even() {
method1();
method2();
method3();
method4();
}
@Scheduled(cron = "0 1/2 * * * *")
public void odd() {
method1();
method3();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论