io.github.kits.timer.TimedTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimthen-kits Show documentation
Show all versions of whimthen-kits Show documentation
Easy to use java tool library.
The newest version!
package io.github.kits.timer;
import java.util.Date;
import java.util.Objects;
import java.util.Timer;
import java.util.function.Consumer;
/**
* @project: kits
* @created: with IDEA
* @author: nzlong
* @date: 2019/02/20 14:28 | February. Wednesday
*/
public class TimedTask {
public static DelayTask delay() {
return DelayTask.getDelayTask();
}
/**
* 从firstTime时刻开始,每隔period毫秒执行一次。
*
* @param taskName
* @param task
* @param firstTime
* @param period
* @param
*/
public static void addFirstTimeTask(String taskName, Consumer task, Date firstTime, long period) {
if (Objects.isNull(firstTime)) {
firstTime = new Date();
}
TimedKit.addTask(taskName, null, null, task, null, period, null, firstTime);
}
/**
* 从firstTime时刻开始,每隔period毫秒执行一次。
*
* @param taskName
* @param task
* @param firstTime
* @param period
* @param
*/
public static void addFirstTimeTask(String taskName, T t, Consumer task, Date firstTime, long period) {
if (Objects.isNull(firstTime)) {
firstTime = new Date();
}
TimedKit.addTask(taskName, t, null, task, null, period, null, firstTime);
}
/**
* 在指定时间执行一次
*
* @param taskName
* @param task
* @param date
* @param
*/
public static void addTask(String taskName, Consumer task, Date date) {
TimedKit.addTask(taskName, null, null, task, null, null, date, null);
}
/**
* 每隔period毫秒执行一次
*
* @param taskName
* @param task
* @param period
* @param
*/
public static void addTask(String taskName, Consumer task, long period) {
TimedKit.addTask(taskName, null, null, task, null, period, null, null);
}
/**
* 每隔period毫秒执行一次
*
* @param taskName
* @param t
* @param task
* @param period
* @param
*/
public static void addTask(String taskName, T t, Consumer task, long period) {
TimedKit.addTask(taskName, t, null, task, null, period, null, null);
}
/**
* 取消定时任务
*
* @param taskName
*/
public static void cancel(String taskName) {
TimedKit.checkTaskName(taskName);
Timer timer = TimedKit.TIMER_MAP.get(taskName);
if (Objects.isNull(timer)) {
throw new NullPointerException("Timer is not exists!");
}
timer.cancel();
}
}