Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring@Scheduled注释按顺序对同一时间实例执行_Java_Spring_Cron_Spring Scheduled - Fatal编程技术网

Java Spring@Scheduled注释按顺序对同一时间实例执行

Java Spring@Scheduled注释按顺序对同一时间实例执行,java,spring,cron,spring-scheduled,Java,Spring,Cron,Spring Scheduled,实际产量: import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.Scheduled; @Configuration public class Scheduler { @Scheduled(cron = "0 */1 * * * ?") public void method1() { Syste

实际产量:

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");
    }
}
我正在接收的输出在同一时间实例上是完全随机的。但我想按以下方式对同一时间实例的输出进行排序:

1
3

1
3
2
4

3
1

1
2
3
4

1
3

3
1
2
4

这是否可以使用相同的Cron表达式实现上述场景?

您必须添加更小的时间单位:

1
3

1
2
3
4

1
3

1
2
3
4
编辑:其他实施:

@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");
}

你不认为,这是对cron的硬编码,只是为了解决获取代码顺序的目的,但是通过引入以秒为单位的延迟来误导时间的准确性吗?我们不能每隔1分钟和2分钟执行一次吗?@tri.akki7不太可能,这取决于你想完成什么。其他解决方案是创建两个方法:一个调用
method1
method3
,另一个按顺序调用所有四个方法并安排它们。我想到的最后一个解决方案是从
method1
等调用
method3
,但这需要更多的逻辑。我如何实现这一点:其他解决方案是创建两个方法:一个调用method1和method3,另一个依次调用所有四个方法,并使用Cron的if语句使用Cron表达式对它们进行调度?你能举个例子吗?如果cron是按以下方式调度的:odd方法:0*/15***?偶数方法:0**?每天12:00时,只有偶数方法运行,而不是奇数方法。除上午12:00外,奇数方法应在一天的其余时间运行。你对此有什么建议?如果订单真的很重要,安排一项任务。。。然后按照应该执行的顺序调用这些方法
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();
}