计时器计划 运行 方法每 15 分钟

Timer Schedule run method Every 15th Minute

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

请告诉我我在哪里 弄错了下面的代码无法正常工作?

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 
        }
    }
}

这是一个工作示例

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

干杯,

根据您的说明,任务需要 运行 恰好在小时的特定分钟(0、15、30、45),我真的建议您查看合适的作业调度库, 例如 Quartz.

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

您可以按照 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");
    }
}
不鼓励

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.

而是使用 ScheduledThreadPoolExecutor:

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