在一个Web项目中,由于需要经常定时去处理一些事情,因此,我们经常在开发中使用定时任务来处理这类的业务逻辑。在Java中,除了使用类似于Spring 等框架携带的定时任务外,我们还可以选择JDK中自带的定时任务功能来实现定时任务。
首先看一下jdk自带定时器:
JDK中的定时任务是一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。
schedule(TimerTask task,long delay) 安排在指定延迟后执行指定的任务。
schedule(TimerTask task,Date time) 安排在指定的时间执行指定的任务。如果此时间已过去,则安排立即执行该任务。
schedule(TimerTask task, long delay, long period) 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟
schedule(TimerTask task,Date firstTime,long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。
一个简单的例子:package test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
/**
* jdk自带定时器
*
* @author LIUTIE
*
*/
public class JDKTimer {
public static void main(String[] args) throws ParseException {
//日期格式工具
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer = new Timer();
// 10s后执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer one will be executed after 10 seconds...");
long milliseconds = 10 * 1000;
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer one has finished execution");
}
}, milliseconds);
//12秒后执行定时器,每1s执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer two will be executed after 12 seconds...");
//启动后延迟时间
long afterSs = 12 * 1000;
//执行周期
long intervalSs1 = 1 * 1000;
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer two has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, afterSs, intervalSs1);
// 指定时间执行定时器,仅执行一次
System.out.print(sdf.format(new Date()));
System.out.println("the timer three will be executed at 2017-06-27 21:47:00...");
Date date = sdf.parse("2017-06-27 21:47:00");
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer three has finished execution");
}
}, date);
// 从指定时间开始周期性执行
System.out.print(sdf.format(new Date()));
System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");
// 执行间隔周期
long intervalSs = 1 * 1000;
// 开始执行时间
Date beginTime = sdf.parse("2017-06-27 21:48:00");
timer.schedule(new TimerTask() {
// 执行计数器
int i = 0;
@Override
public void run() {
System.out.print(sdf.format(new Date()));
System.out.println("the timer four has execution " + (++i) + " timers");
// 执行10次后关闭定时器
if (i == 10) {
this.cancel();
}
}
}, beginTime, intervalSs);
}
}
继续来一个优化后的例子:import java.util.TimerTask;
/**
* 实现定时任务
*
*/
public class MyTimerTask extends TimerTask {
@Override
public void run() {
int num = 0;
while (num
num++;
System.out.println(num + " Test...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}
还有一种方式也能通过JDK自带的功能实现定时任务:import java.util.TimerTask;
/**
* 启动定时任务
*
*/
public class MyTimer extends java.util.Timer {
/**
* task:实现的定时任务 delay:项目启动制定时间后开始执行任务 period:没有执行定时任务的间隔时间
* 如果Task执行时间过长;会导致period执行任务延期
*/
public void schedule(TimerTask task, long delay, long period) {
super.schedule(task, delay, period);
}
/**
* Java启动定时任务
*
* @param arg
*/
public static void main(String arg[]) {
MyTimerTask task = new MyTimerTask();
new MyTimer().schedule(task, 1000, 5000);
}
}