多线程:5.定时器Timer
2016-09-04 本文已影响67人
81bad73e9053
1.schedule(timertask,time)
1.1晚于当前时间执行:
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 = "2016-09-04 09:00:00";
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task, dateRef);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:00:00 当前时间:2016-9-4 8:59:15
运行了!时间为:Sun Sep 04 09:00:00 CST 2016
Timer的构造方法:timer就是构造一个新的线程
public Timer(){
this("Timer-"+serialNumber());
}
public Timer(String name){
thread.setName(name);
thread.start();
}
创建Timer为守护线程
public class Run1TimerIsDaemon {
private static Timer timer = new Timer(true);
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 = "2016-09-04 09:04:55";
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task, dateRef);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:守护线程,当前进程结束,线程也就结束了,timertask没有执行
字符串时间:2016-9-4 9:04:55 当前时间:2016-9-4 9:04:22
1.2计划时间早于当前时间:立即执行task任务
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 = "2016-09-04 09:00:00";
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task, dateRef);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:00:00 当前时间:2016-9-4 9:07:18
运行了!时间为:Sun Sep 04 09:07:18 CST 2016
1.3多个task
public class Run2 {
private static Timer timer = new Timer();
static public class MyTask1 extends TimerTask {
@Override
public void run() {
System.out.println("运行了!时间为:" + new Date());
}
}
static public class MyTask2 extends TimerTask {
@Override
public void run() {
System.out.println("运行了!时间为:" + new Date());
}
}
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 = "2016-09-04 09:25:00";
String dateString2 = "2016-09-04 09:26: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();
}
}
}
运行结果:
字符串1时间:2016-9-4 9:25:00 当前时间:2016-9-4 9:24:03
字符串2时间:2016-9-4 9:26:00 当前时间:2016-9-4 9:24:03
运行了!时间为:Sun Sep 04 09:25:00 CST 2016
运行了!时间为:Sun Sep 04 09:26:00 CST 2016
1.4多个task任务,第一个任务执行时间很长
public class Run2Later {
private static Timer timer = new Timer();
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了!时间为:" + new Date());
Thread.sleep(40000);
System.out.println("1 end 运行了!时间为:" + new Date());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static public class MyTask2 extends TimerTask {
@Override
public void run() {
System.out.println("2 begin 运行了!时间为:" + new Date());
System.out.println("运行了!时间为:" + new Date());
System.out.println("2 end 运行了!时间为:" + new Date());
}
}
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 = "2016-09-04 09:28:00";
String dateString2 = "2016-09-04 09:28:30";
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();
}
}
}
运行结果:
task2没有在指定时间执行
1.5schedule(task,time,period)
周期性循环执行某一个任务
1.5.1计划时间晚于当前时间
public class Run {
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 = "2016-09-04 09:37:00";
Timer timer = new Timer();
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task, dateRef, 2000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果 :
字符串时间:2016-9-4 9:37:00 当前时间:2016-9-4 9:36:47
运行了!时间为:Sun Sep 04 09:37:00 CST 2016
运行了!时间为:Sun Sep 04 09:37:02 CST 2016
运行了!时间为:Sun Sep 04 09:37:04 CST 2016
运行了!时间为:Sun Sep 04 09:37:06 CST 2016
运行了!时间为:Sun Sep 04 09:37:08 CST 2016
运行了!时间为:Sun Sep 04 09:37:10 CST 2016
1.5.2计划时间早于当前时间:立即执行
public class Run {
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 = "2016-09-04 09:37:00";
Timer timer = new Timer();
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task, dateRef, 2000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:37:00 当前时间:2016-9-4 9:38:02
运行了!时间为:Sun Sep 04 09:38:02 CST 2016
运行了!时间为:Sun Sep 04 09:38:04 CST 2016
运行了!时间为:Sun Sep 04 09:38:06 CST 2016
运行了!时间为:Sun Sep 04 09:38:08 CST 2016
运行了!时间为:Sun Sep 04 09:38:10 CST 2016
1.5.3任务执行时间被延时
public class Run2_1 {
static public class MyTaskA extends TimerTask {
@Override
public void run() {
try {
System.out.println("A运行了!时间为:" + new Date());
Thread.sleep(5000);
System.out.println("A结束了!时间为:" + new Date());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTaskA taskA = new MyTaskA();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2016-09-04 09:47:00";
Timer timer = new Timer();
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(taskA, dateRef, 3000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:47:00 当前时间:2016-9-4 9:46:07
A运行了!时间为:Sun Sep 04 09:47:00 CST 2016
A结束了!时间为:Sun Sep 04 09:47:05 CST 2016
A运行了!时间为:Sun Sep 04 09:47:05 CST 2016
A结束了!时间为:Sun Sep 04 09:47:10 CST 2016
A运行了!时间为:Sun Sep 04 09:47:10 CST 2016
A结束了!时间为:Sun Sep 04 09:47:15 CST 2016
A运行了!时间为:Sun Sep 04 09:47:15 CST 2016
A结束了!时间为:Sun Sep 04 09:47:20 CST 2016
1.5.4task的cancel
public class Run2 {
static public class MyTaskA extends TimerTask {
@Override
public void run() {
System.out.println("A运行了!时间为:" + new Date());
this.cancel();
}
}
static public class MyTaskB extends TimerTask {
@Override
public void run() {
System.out.println("B运行了!时间为:" + new Date());
}
}
public static void main(String[] args) {
try {
MyTaskA taskA = new MyTaskA();
MyTaskB taskB = new MyTaskB();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2016-09-04 09:50:00";
Timer timer = new Timer();
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(taskA, dateRef, 4000);
timer.schedule(taskB, dateRef, 4000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:50:00 当前时间:2016-9-4 9:49:52
A运行了!时间为:Sun Sep 04 09:50:00 CST 2016
B运行了!时间为:Sun Sep 04 09:50:00 CST 2016
B运行了!时间为:Sun Sep 04 09:50:04 CST 2016
B运行了!时间为:Sun Sep 04 09:50:08 CST 2016
B运行了!时间为:Sun Sep 04 09:50:12 CST 2016
1.5.5timer的cancel
public class Run3 {
private static Timer timer = new Timer();
static public class MyTaskA extends TimerTask {
@Override
public void run() {
System.out.println("A运行了!时间为:" + new Date());
timer.cancel();
}
}
static public class MyTaskB extends TimerTask {
@Override
public void run() {
System.out.println("B运行了!时间为:" + new Date());
}
}
public static void main(String[] args) {
try {
MyTaskA taskA = new MyTaskA();
MyTaskB taskB = new MyTaskB();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = "2016-09-04 09:52:00";
Date dateRef = sdf.parse(dateString);
System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(taskA, dateRef, 4000);
timer.schedule(taskB, dateRef, 4000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串时间:2016-9-4 9:52:00 当前时间:2016-9-4 9:51:13
A运行了!时间为:Sun Sep 04 09:52:00 CST 2016
timer.cancel的注意事项
1.6 schedule(task,delay)的使用
public class Run {
static public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("运行了!时间为:" + new Date());
}
}
public static void main(String[] args) {
MyTask task = new MyTask();
Timer timer = new Timer();
System.out.println("当前时间:" + new Date().toLocaleString());
timer.schedule(task, 3000);
}
}
运行结果:
当前时间:2016-9-4 21:44:18
运行了!时间为:Sun Sep 04 21:44:21 CST 2016
1.7 schedule(task,delay,period)
public class Run {
static public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("运行了!时间为:" + new Date());
}
}
public static void main(String[] args) {
MyTask task = new MyTask();
Timer timer = new Timer();
System.out.println("当前时间:"+new Date().toLocaleString());
timer.schedule(task, 3000,2000);
}
}
运行结果:
当前时间:2016-9-4 21:46:35
运行了!时间为:Sun Sep 04 21:46:38 CST 2016
运行了!时间为:Sun Sep 04 21:46:40 CST 2016
运行了!时间为:Sun Sep 04 21:46:42 CST 2016
运行了!时间为:Sun Sep 04 21:46:44 CST 2016
运行了!时间为:Sun Sep 04 21:46:46 CST 2016
1.7 scheduleAtFiexedRate(task,firstTime,period)
schedule:
scheduleAtFiexedRate:
1.7.1测试schedule方法任务不延时
如果执行任务时间没有被延时,则下一次执行任务的开始时间加上delay时间
public class Run1 {
private static Timer timer = new Timer();
private static int runCount = 0;
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了!时间为:" + new Date());
Thread.sleep(1000);
System.out.println("1 end 运行了!时间为:" + new Date());
runCount++;
if (runCount == 5) {
timer.cancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTask1 task1 = new MyTask1();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2016-09-04 21:56:00";
Date dateRef1 = sdf1.parse(dateString1);
System.out.println("字符串1时间:" + dateRef1.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task1, dateRef1, 3000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串1时间:2016-9-4 21:56:00 当前时间:2016-9-4 21:55:27
1 begin 运行了!时间为:Sun Sep 04 21:56:00 CST 2016
1 end 运行了!时间为:Sun Sep 04 21:56:01 CST 2016
1 begin 运行了!时间为:Sun Sep 04 21:56:03 CST 2016
1 end 运行了!时间为:Sun Sep 04 21:56:04 CST 2016
1.7.2如果执行任务被延时,那么下一次任务的执行时间以上一次执行任务的结束时间为参考
public class Run2 {
private static Timer timer = new Timer();
private static int runCount = 0;
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了!时间为:" + new Date());
Thread.sleep(5000);
System.out.println("1 end 运行了!时间为:" + new Date());
runCount++;
if (runCount == 5) {
timer.cancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTask1 task1 = new MyTask1();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2016-09-04 22:00:00";
Date dateRef1 = sdf1.parse(dateString1);
System.out.println("字符串1时间:" + dateRef1.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.schedule(task1, dateRef1, 2000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串1时间:2016-9-4 22:00:00 当前时间:2016-9-4 21:59:17
1 begin 运行了!时间为:Sun Sep 04 22:00:00 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:00:05 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:00:05 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:00:10 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:00:10 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:00:15 CST 2016
1.7.3scheduleAtFixedRate测试不延时
如果执行任务时间没有被延时,则下一次执行任务的时间是上一次执行任务的开始时间加上delay时间
public class Run3 {
private static Timer timer = new Timer();
private static int runCount = 0;
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了!时间为:" + new Date());
Thread.sleep(2000);
System.out.println("1 end 运行了!时间为:" + new Date());
runCount++;
if (runCount == 5) {
timer.cancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTask1 task1 = new MyTask1();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2016-09-04 22:03:00";
Date dateRef1 = sdf1.parse(dateString1);
System.out.println("字符串1时间:" + dateRef1.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.scheduleAtFixedRate(task1, dateRef1, 3000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串1时间:2016-9-4 22:03:00 当前时间:2016-9-4 22:02:50
1 begin 运行了!时间为:Sun Sep 04 22:03:00 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:03:02 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:03:03 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:03:05 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:03:06 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:03:08 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:03:09 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:03:11 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:03:12 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:03:14 CST 2016
1.7.4scheduleAtFixedRate测试延时任务
如果执行任务被延时,那么下一次任务的执行时间以上一次任务的结束时间为参考
public class Run4 {
private static Timer timer = new Timer();
private static int runCount = 0;
static public class MyTask1 extends TimerTask {
@Override
public void run() {
try {
System.out.println("1 begin 运行了!时间为:" + new Date());
Thread.sleep(5000);
System.out.println("1 end 运行了!时间为:" + new Date());
runCount++;
if (runCount == 5) {
timer.cancel();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
MyTask1 task1 = new MyTask1();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString1 = "2016-09-04 22:08:00";
Date dateRef1 = sdf1.parse(dateString1);
System.out.println("字符串1时间:" + dateRef1.toLocaleString() + " 当前时间:"
+ new Date().toLocaleString());
timer.scheduleAtFixedRate(task1, dateRef1, 2000);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
运行结果:
字符串1时间:2016-9-4 22:08:00 当前时间:2016-9-4 22:07:19
1 begin 运行了!时间为:Sun Sep 04 22:08:00 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:08:05 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:08:05 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:08:10 CST 2016
1 begin 运行了!时间为:Sun Sep 04 22:08:10 CST 2016
1 end 运行了!时间为:Sun Sep 04 22:08:15 CST 2016
1.7.5schedule方法不具有追赶执行性
如果任务开始时间小于当前时间,那么开始时间到当前时间这个时间段内的任务不会被执行
1.7.6scheduleAtFixedRate具有追赶性
如果任务开始时间小于当前时间,那么开始时间到当前时间这个时间段内的任务会被执行