第5章 定时器Timer
标签: Java多线程编程
《Java多线程编程核心技术》 个人笔记
第5章 定时器Timer
定时器Timer的使用
方法scheduleTimerTask task Date time的测试方法scheduleTimerTask task Date firstTime long period的测试方法scheduleTimerTask task long delay的测试方法scheduleTimerTask task long delay long period的测试方法scheduleAtFixedRateTimerTask task Date firstTime long period的测试追赶性
本章着重掌握一下技术点: 1. 如何实现指定时间执行任务 2. 如何实现按指定周期执行任务
定时器Timer的使用
在JDK库中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务。Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类
方法schedule(TimerTask task, Date time)的测试
该方法的作用是在指定的日期执行一次某一任务
public class Run1 {
private static Timer timer =
new Timer();
static public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println(
"运行了!时间为:" +
new Date());
}
}
public static void main(String[] args) {
try {
MyTask task =
new MyTask();
SimpleDateFormat sdf =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String dateString =
"2014-10-12 11:55:00";
Date dateRef = sdf.parse(dateString);
System.out.println(
"字符串时间:" + dateRef.toLocaleString() +
" 当前时间:"
+
new Date().toLocaleString());
timer.schedule(task, dateRef);
}
catch (ParseException e) {
e.printStackTrace();
}
}
}
将定时器设置为守护线程,构造参数传入true:private static Timer timer = new Timer(True); 如果计划时间早于当前时间,则立刻执行task任务Timer中允许有多个TimerTask任务
public static
void main(
String[] args) {
try {
MyTask1 task1 =
new MyTask1();
MyTask2 task2 =
new MyTask2();
SimpleDateFormat sdf1 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String dateString1 =
"2014-10-12 10:39:00";
String dateString2 =
"2014-10-12 10:40:00";
Date dateRef1 = sdf1.parse(dateString1);
Date dateRef2 = sdf2.parse(dateString2);
System.out.println(
"字符串1时间:" + dateRef1.toLocaleString() +
" 当前时间:"
+
new Date().toLocaleString());
System.out.println(
"字符串2时间:" + dateRef2.toLocaleString() +
" 当前时间:"
+
new Date().toLocaleString());
timer.schedule(task1, dateRef1);
timer.schedule(task2, dateRef2);
}
catch (ParseException e) {
e.printStackTrace();
}
}
TimerTask是以队列的方式一个一个被顺序执行的,所以执行的时间有可能和预期的时间不一致,因为前面的任务有可能消耗的时间较长,则后面的任务运行的时间也会被延迟
方法schedule(TimerTask task, Date firstTime, long period)的测试
该方法的作用是在指定的日期之后,按指定的建个周期性地无限循环地执行某一任务
timer.schedule(
task, dateRef,
4000);
TimerTask类的方法cancel()的作用是将自身从任务队列中清除,其他任务不受影响
static public class MyTaskA extends TimerTask {
@Override
public void run() {
System.out.println(
"A运行了!时间为:" +
new Date());
this.cancel();
}
}
和TimerTask类的方法cancel()不同,Timer类中的cancel()方法的作用是将任务队列中的全部任务清空,并且进程被销毁
private static Timer timer =
new Timer();
static public class MyTaskA extends TimerTask {
@Override
public void run() {
System.out.println(
"A运行了!时间为:" +
new Date());
timer.cancel();
}
}
因为Timer类中的cancel()放油有时并没有强盗queue锁,所以有时并不一定会停止执行计划任务
方法schedule(TimerTask task, long delay)的测试
该方法是以执行schedule(TimerTask task, long delay)方法当前的时间为参考时间,在此时间基础上延迟指定的毫秒数后执行一次TimerTask任务
public static void main(
String[] args) {
MyTask task =
new MyTask();
Timer timer =
new Timer();
System.out.println(
"当前时间:" +
new Date().toLocaleString());
timer.schedule(task,
7000);
}
方法schedule(TimerTask task, long delay, long period)的测试
该方法的作用是以执行schedule(TimerTask task, long delay, long period)方法当前的时间为参考时间,在此时间基础上延迟指定的毫秒数,再以某一间隔时间无线次数地执行某一任务
public static void main(
String[] args) {
MyTask task =
new MyTask();
Timer timer =
new Timer();
System.out.println(
"当前时间:"+
new Date().toLocaleString());
timer.schedule(task,
3000,
5000);
}
方法scheduleAtFixedRate(TimerTask task, Date firstTime, long period)的测试
使用schedule方法:如果执行任务的时间没有被延时,那么下一次任务的执行时间参考的是上一次任务的“开始”时的时间来计算使用scheduleAtFixedRate方法:如果执行任务的时间没有被延时,那么下一次任务的执行时间参考的是上一次任务的“结束”的时间来计算延时则没有区别,都是参考上一次任务“结束”时的时间来计算
追赶性