阅读背景:

定时任务选型,你也许有更好的选择?-linux定时任务crontab

来源:互联网 


一个系统刚刚构建的时候,往往需要定时执行的任务,但没有,有人就推荐java的timer

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

public class TimerTest extends TimerTask {

private String jobName = "";

public TimerTest(String jobName) {
super();
this.jobName = jobName;
}

@Override
public void run() {
System.out.println("execute " + jobName);
}

public static void main(String[] args) {
Timer timer = new Timer();
long delay1 = 1 * 1000;
long period1 = 1000;
// 从现在开始 1 秒钟之后,每隔 1 秒钟执行一次 job1
timer.schedule(new TimerTest("job1"), delay1, period1);
long delay2 = 2 * 1000;
long period2 = 2000;
// 从现在开始 2 秒钟之后,每隔 2 秒钟执行一次 job2
timer.schedule(new TimerTest("job2"), delay2, period2);
}
}im



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: