java Timer Schedule 运行方法 每 15 分钟

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30047893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:21:21  来源:igfitidea点击:

Timer Schedule run method Every 15th Minute

javatimer

提问by sameer

I would like to run method every 15 minutes for example 0:15,0:30,0:45,1:00,1:15,1:30 etc.

我想每 15 分钟运行一次方法,例如 0:15,0:30,0:45,1:00,1:15,1:30 等。

Please let me know where I have made mistaken below code not working properly?

请让我知道我在下面的代码中出错的地方无法正常工作?

public class MainClass{
    //Set Calendar
    Calendar calendar =  Calendar.getInstance();
    calendar.set(Calendar.MINUTE , 15);
    private Timer timer;
    switch(flag) {  //here flag 1 ,2 etc
    case 1: //Initial Server 
    timer.schedule(new MyTask(),0);
    break;
    case:2
    timer.schedule(new MyTask(),calendar.getTime(),Long.valueOf(1)*1000*900);
    break;
    }  
    class MyTask extends TimerTask{
        public void run() {
        //Method Stuff here 
        }
    }
}

回答by ktorn

Following up on your clarification that the task needs to run exactly at the specific minutes of the hour (0, 15, 30, 45), I really recommend that you look into a proper job scheduling library, such as Quartz.

根据您的澄清,任务需要精确地在每小时的特定分钟数(0、15、30、45)运行,我真的建议您查看适当的作业调度库,例如Quartz

On the other hand, if you really want to stick to using Timer, your problem is really to find the correct first running time for your timer, which can then run every 15 min from then on.

另一方面,如果你真的想坚持使用 Timer,你的问题实际上是为你的计时器找到正确的第一次运行时间,然后它可以每 15 分钟运行一次。

You can do it with Joda, as suggested by Ben, but this code will probably work for you:

您可以按照 Ben 的建议使用 Joda 来完成此操作,但此代码可能对您有用:

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class TimerExample {

    private static Timer timer = new Timer();

    private static Calendar getFirstTime() {
        Calendar cal = Calendar.getInstance();

        int currentMinute = cal.get(Calendar.MINUTE);

        if (currentMinute < 45) {
            cal.set(Calendar.MINUTE, 45);
        }
        if (currentMinute < 30) {
            cal.set(Calendar.MINUTE, 30);
        }
        if (currentMinute < 15) {
            cal.set(Calendar.MINUTE, 15);
        }
        if (currentMinute >= 45) {
            cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + 1);
            cal.set(Calendar.MINUTE, 0);
        }

        cal.set(Calendar.SECOND, 0);

        return cal;
    }

    public static void main(String... args) {
        Calendar firstTaskTime = getFirstTime();
        System.out.println("Task will start at: " + firstTaskTime.getTime());
        timer.schedule(new MyTask(), firstTaskTime.getTime(), 1000 * 60 * 15);
    }
}

class MyTask extends TimerTask {
    public void run() {
        System.out.println("running task");
    }
}

回答by u290629

Timeris not encouraged, see javadoc:

Timer不鼓励,请参阅 javadoc:

Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

Java 5.0 引入了 java.util.concurrent 包,其中的并发实用程序之一是 ScheduledThreadPoolExecutor,它是一个线程池,用于以给定的速率或延迟重复执行任务。它实际上是 Timer/TimerTask 组合的更通用的替代品,因为它允许多个服务线程,接受各种时间单位,并且不需要子类化 TimerTask(只需实现 Runnable)。使用一个线程配置 ScheduledThreadPoolExecutor 使其等效于 Timer。

Instead, use ScheduledThreadPoolExecutor:

相反,使用ScheduledThreadPoolExecutor

 Executors.newScheduledThreadPool(n)
.scheduleAtFixedRate(()->{...}, 0, 15, TimeUnit.MINUTES)

回答by Ben Rhouma Zied

Here is a working example

这是一个工作示例

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainClass {

    private static Timer timer = new Timer();

    public static void main(String...args){
           timer.schedule (new MyTask(),0,1000*60*15);
    }
}

class MyTask extends TimerTask {
        public void run() {
            System.out.println("hello");
        }
    }

Cheers,

干杯,