Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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在特定时间段后每次运行特定方法_Java - Fatal编程技术网

需要使用java在特定时间段后每次运行特定方法

需要使用java在特定时间段后每次运行特定方法,java,Java,我需要自动化我的API案例,在这些案例中,我用来运行API的令牌在每一个小时后就会过期。所以我需要使用特定的方法重新生成令牌。当我运行自动化时,如何在每小时后运行此特定方法 你可以试试睡眠法 new Thread(()->{ while(true) { method(); //your method Thread.sleep(3600000); } }).start(); 您可以使用类似cron的方法“scheduleAtFixedRate

我需要自动化我的API案例,在这些案例中,我用来运行API的令牌在每一个小时后就会过期。所以我需要使用特定的方法重新生成令牌。当我运行自动化时,如何在每小时后运行此特定方法

你可以试试睡眠法

new Thread(()->{
    while(true) {
        method(); //your method
        Thread.sleep(3600000);
    }
}).start();

您可以使用类似cron的方法“scheduleAtFixedRate”


您只需使用EnableScheduling即可

类似的东西应该可以做到这一点(改编自Javadoc for@EnableScheduling):

另一种方法是将@Scheduled(cron=“0 0/1 1/1*?”)放在api调用方法之前,如下所示:

@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {

    long now = System.currentTimeMillis() / 1000;
    System.out.println(
      "schedule tasks using cron jobs - " + now);
}

使用调度程序:
sleep()
使用毫秒,因此这将是3.6秒。是的,我已经解决了
@Scheduled(cron = "0 15 10 15 * ?")
public void scheduleTaskUsingCronExpression() {

    long now = System.currentTimeMillis() / 1000;
    System.out.println(
      "schedule tasks using cron jobs - " + now);
}