java 定时器工具类

来源:互联网 发布:高铁争夺战 知乎 编辑:程序博客网 时间:2024/05/16 05:02
package util;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class UtilTimer {

private static Timer timer;

/**
* delay时间后开始执行任务,并每隔period时间调用任务一次。
* @param task
* @param delay
* @param period
*/
public static void timer(TimerTask task,long delay,long period){

timer = new Timer(true);

timer.schedule(task, delay, period);
}

/**
* 指定日期执行任务
* @param task
* @param date
*/
public static void timer(TimerTask task,Date date){

timer = new Timer(true);

timer.schedule(task, date);

}


/**
* 多长时间(毫秒)后执行任务
* @param task
* @param delay
*/
public static void timer(TimerTask task,long delay){

timer = new Timer(true);

timer.schedule(task, delay);
}


/**
* 第一次在指定firstTime时间点执行任务,之后每隔period时间调用任务一次。
* @param task
* @param firstTime
* @param period
*/
public static void timer(TimerTask task, Date firstTime, long period){

timer = new Timer(true);

timer.scheduleAtFixedRate(task,firstTime,period);
}

/**
* 终止定时器
*/
public void cancelTimer(){

timer.cancel();

}

/**
* 终止任务
* @param task
*/
public void cancelTimerTask(TimerTask task){

task.cancel();
}




}



原创粉丝点击