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);
}
}
2017-06-27 21:46:24the timer one will be executed after 10 seconds... 2017-06-27 21:46:24the timer two will be executed after 12 seconds... 2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00... 2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00... 2017-06-27 21:46:34the timer one has finished execution 2017-06-27 21:46:36the timer two has execution 1 timers 2017-06-27 21:46:37the timer two has execution 2 timers 2017-06-27 21:46:38the timer two has execution 3 timers 2017-06-27 21:46:39the timer two has execution 4 timers 2017-06-27 21:46:40the timer two has execution 5 timers 2017-06-27 21:46:41the timer two has execution 6 timers 2017-06-27 21:46:42the timer two has execution 7 timers 2017-06-27 21:46:43the timer two has execution 8 timers 2017-06-27 21:46:44the timer two has execution 9 timers 2017-06-27 21:46:45the timer two has execution 10 timers 2017-06-27 21:47:00the timer three has finished execution 2017-06-27 21:48:00the timer four has execution 1 timers 2017-06-27 21:48:01the timer four has execution 2 timers 2017-06-27 21:48:02the timer four has execution 3 timers 2017-06-27 21:48:03the timer four has execution 4 timers 2017-06-27 21:48:04the timer four has execution 5 timers 2017-06-27 21:48:05the timer four has execution 6 timers 2017-06-27 21:48:06the timer four has execution 7 timers 2017-06-27 21:48:07the timer four has execution 8 timers 2017-06-27 21:48:08the timer four has execution 9 timers 2017-06-27 21:48:09the timer four has execution 10 timers
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有