欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java定时器Timer简述

程序员文章站 2024-03-08 10:07:34
概述 主要用于java线程里指定时间或周期运行任务。timer是线程安全的,但不提供实时性(real-time)保证。 构造函数 timer() 默认构造函数。...

概述

主要用于java线程里指定时间或周期运行任务。timer是线程安全的,但不提供实时性(real-time)保证。

构造函数

timer()

默认构造函数。

timer(boolean)

指定关联线程是否作为daemon线程。

timer(string)

指定关联线程的名称。

timer(string, boolean)

同时指定关联线程的名称和是否作为daemon。

schdule方法

schedule(timertask task, long delay)

以当前时间为基准,延迟指定的毫秒后执行一次timertask任务。

schedule(timertask task, date time)

在指定的日期执行一次timertask任务。

如果日期time早于当前时间,则立刻执行。

使用示例

public class demo {
 private static timer timer = new timer();
 public static class mytask extends timertask {
 @override
 public void run() {
  system.out.println("run time:" + new date().tostring());
 }
 }
 public static void main(string[] args) {
 try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 14:36:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.schedule(task, date);
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }
}

执行结果

date = tue dec 27 14:36:00 cst 2016 nowtime = tue dec 27 21:28:04 cst 2016
run time:tue dec 27 21:28:04 cst 2016

说明是立刻执行。

schedule(timertask task, long delay, long period)

以当前时间为基准,延迟指定的毫秒后,再按指定的时间间隔地无限次数的执行timertask任务。(fixed-delay execution)

使用示例

public class demo {
 private static timer timer = new timer();
 public static class mytask extends timertask {
 @override
 public void run() {
  system.out.println("run time: " + new date().tostring());
 }
 }
 public static void main(string[] args) {
 mytask task = new mytask();
 system.out.println("now time: " + new date().tostring());
 timer.schedule(task, 3000, 5000);
 }
}

执行结果

now time: tue dec 27 21:34:59 cst 2016
run time: tue dec 27 21:35:02 cst 2016
run time: tue dec 27 21:35:07 cst 2016
run time: tue dec 27 21:35:12 cst 2016
run time: tue dec 27 21:35:17 cst 2016

说明以当前基准时间延迟3秒后执行一次,以后按指定间隔时间5秒无限次数的执行。

schedule(timertask task, date firsttime, long period)

在指定的日期之后,按指定的时间间隔地无限次数的执行timertask任务。(fixed-delay execution)

如果日期firsttime早于当前时间,则立刻执行,且不执行在时间差内的任务。

使用示例

public class demo {
 private static timer timer = new timer();
 public static class mytask extends timertask {
 @override
 public void run() {
  system.out.println("run time:" + new date().tostring());
 }
 public static void main(string[] args) {
  try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 14:36:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.schedule(task, date, 3000);
  } catch (parseexception e) {
  e.printstacktrace();
  }
 }
 }
}

执行结果

date = tue dec 27 14:36:00 cst 2016 nowtime = tue dec 27 21:43:30 cst 2016
run time:tue dec 27 21:43:30 cst 2016
run time:tue dec 27 21:43:33 cst 2016
run time:tue dec 27 21:43:36 cst 2016

说明指定的之间早于当前时间,则立刻执行,不会补充时间差内的任务

scheduleatfixedrate方法

scheduleatfixedrate(timertask task, long delay, long period)

以当前时间为基准,延迟指定的毫秒后,再按指定的时间间隔周期性地无限次数的执行timertask任务。(fixed-rate execution)

使用示例

public class demo {
 private static timer timer = new timer();
 public static class mytask extends timertask {
 @override
 public void run() {
  system.out.println("run time: " + new date().tostring());
 }
 }
 public static void main(string[] args) {
 mytask task = new mytask();
 system.out.println("now time: " + new date().tostring());
 timer.scheduleatfixedrate(task, 3000, 5000);
 }
}

执行结果

now time: tue dec 27 21:58:03 cst 2016
run time: tue dec 27 21:58:06 cst 2016
run time: tue dec 27 21:58:11 cst 2016
run time: tue dec 27 21:58:16 cst 2016
run time: tue dec 27 21:58:21 cst 2016

说明以当前基准时间延迟3秒后执行一次,以后按指定间隔时间5秒无限次数的执行。

scheduleatfixedrate(timertask task, date firsttime, long period)

在指定的日期之后,按指定的时间间隔周期性地无限次数的执行timertask任务。(fixed-rate execution)

如果日期firsttime早于当前时间,则立即执行,并补充性的执行在时间差内的任务。

使用示例

public class demo {
 private static timer timer = new timer();
 public static class mytask extends timertask {
 @override
 public void run() {
  system.out.println("run time:" + new date().tostring());
 }
 public static void main(string[] args) {
  try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 22:02:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.scheduleatfixedrate(task, date, 5000);
  } catch (parseexception e) {
  e.printstacktrace();
  }
 }
 }
}

执行结果

date = tue dec 27 22:02:00 cst 2016 nowtime = tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:54 cst 2016
run time:tue dec 27 22:02:55 cst 2016
run time:tue dec 27 22:03:00 cst 2016
run time:tue dec 27 22:03:05 cst 2016

说明指定的之间早于当前时间,则立刻执行。

在时间22:02:00--22:02:54内大约有11个5秒间隔,则优先补充性的执行在时间差内的任务,然后在22:02:55补充完毕(执行12次。ps:0-55秒区间段内首位都算上,正好触发12次),此后每隔5秒执行一次定时任务。

执行任务延时对比之 schedule 和 scheduleatfixedrate

schedule不延时

使用示例

public class demo {
 private static timer timer = new timer();
 private static int runcount = 0;
 public static class mytask extends timertask {
 @override
 public void run() {
  try {
  system.out.println("begin run time: " + new date().tostring());
  thread.sleep(3000);
  system.out.println("end run time: " + new date().tostring());
  runcount++;
  if (runcount == 3) {
   timer.cancel();
  }
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }
 public static void main(string[] args) {
 try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 14:36:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.schedule(task, date, 5000);
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }
}

执行结果

早于当前基准时间

date = tue dec 27 14:36:00 cst 2016 nowtime = tue dec 27 22:23:37 cst 2016
begin run time: tue dec 27 22:23:37 cst 2016
end run time: tue dec 27 22:23:40 cst 2016
begin run time: tue dec 27 22:23:42 cst 2016
end run time: tue dec 27 22:23:45 cst 2016
begin run time: tue dec 27 22:23:47 cst 2016
end run time: tue dec 27 22:23:50 cst 2016

process finished with exit code 0

晚于当前基准时间

date = tue dec 27 22:42:00 cst 2016 nowtime = tue dec 27 22:41:54 cst 2016
begin run time: tue dec 27 22:42:00 cst 2016
end run time: tue dec 27 22:42:03 cst 2016
begin run time: tue dec 27 22:42:05 cst 2016
end run time: tue dec 27 22:42:08 cst 2016
begin run time: tue dec 27 22:42:10 cst 2016
end run time: tue dec 27 22:42:13 cst 2016

process finished with exit code 0

不管早还是晚于基准时间,都不进行补偿,下一次任务的执行时间参考的是上一次任务的开始时间点来计算。

schedule延时

使用示例

public class demo {
 private static timer timer = new timer();
 private static int runcount = 0;
 public static class mytask extends timertask {
 @override
 public void run() {
  try {
  system.out.println("begin run time: " + new date().tostring());
  thread.sleep(5000);
  system.out.println("end run time: " + new date().tostring());
  runcount++;
  if (runcount == 3) {
   timer.cancel();
  }
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }
 public static void main(string[] args) {
 try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 22:42:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.schedule(task, date, 3000);
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }
}

执行结果

早于当前基准时间

date = tue dec 27 22:42:00 cst 2016 nowtime = tue dec 27 22:45:17 cst 2016
begin run time: tue dec 27 22:45:17 cst 2016
end run time: tue dec 27 22:45:22 cst 2016
begin run time: tue dec 27 22:45:22 cst 2016
end run time: tue dec 27 22:45:27 cst 2016
begin run time: tue dec 27 22:45:27 cst 2016
end run time: tue dec 27 22:45:32 cst 2016

process finished with exit code 0

晚于当前基准时间

date = tue dec 27 22:47:00 cst 2016 nowtime = tue dec 27 22:46:27 cst 2016
begin run time: tue dec 27 22:47:00 cst 2016
end run time: tue dec 27 22:47:05 cst 2016
begin run time: tue dec 27 22:47:05 cst 2016
end run time: tue dec 27 22:47:10 cst 2016
begin run time: tue dec 27 22:47:10 cst 2016
end run time: tue dec 27 22:47:15 cst 2016

process finished with exit code 0

不管早还是晚于当前基准时间,都不进行补偿,下一次任务的执行时间都是参考上一次任务结束的时间点来计算。

scheduleatfixedrate不延时

使用示例

public class demo {
 private static timer timer = new timer();
 private static int runcount = 0;
 public static class mytask extends timertask {
 @override
 public void run() {
  try {
  system.out.println("begin run time: " + new date().tostring());
  thread.sleep(3000);
  system.out.println("end run time: " + new date().tostring());
  runcount++;
  if (runcount == 1000) {
   timer.cancel();
  }
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }
 public static void main(string[] args) {
 try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 22:51:42";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.scheduleatfixedrate(task, date, 5000);
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }
}

执行结果

早于当前基准时间

date = tue dec 27 22:51:42 cst 2016 nowtime = tue dec 27 22:51:57 cst 2016
begin run time: tue dec 27 22:51:57 cst 2016
end run time: tue dec 27 22:52:00 cst 2016
begin run time: tue dec 27 22:52:00 cst 2016
end run time: tue dec 27 22:52:03 cst 2016
begin run time: tue dec 27 22:52:03 cst 2016
end run time: tue dec 27 22:52:06 cst 2016
begin run time: tue dec 27 22:52:06 cst 2016
end run time: tue dec 27 22:52:09 cst 2016
begin run time: tue dec 27 22:52:09 cst 2016
end run time: tue dec 27 22:52:12 cst 2016
begin run time: tue dec 27 22:52:12 cst 2016
end run time: tue dec 27 22:52:15 cst 2016
begin run time: tue dec 27 22:52:15 cst 2016
end run time: tue dec 27 22:52:18 cst 2016
begin run time: tue dec 27 22:52:18 cst 2016
end run time: tue dec 27 22:52:21 cst 2016
begin run time: tue dec 27 22:52:22 cst 2016
end run time: tue dec 27 22:52:25 cst 2016
begin run time: tue dec 27 22:52:27 cst 2016
end run time: tue dec 27 22:52:30 cst 2016
begin run time: tue dec 27 22:52:32 cst 2016
end run time: tue dec 27 22:52:35 cst 2016
begin run time: tue dec 27 22:52:37 cst 2016
end run time: tue dec 27 22:52:40 cst 2016
begin run time: tue dec 27 22:52:42 cst 2016
end run time: tue dec 27 22:52:45 cst 2016
begin run time: tue dec 27 22:52:47 cst 2016
end run time: tue dec 27 22:52:50 cst 2016
begin run time: tue dec 27 22:52:52 cst 2016
end run time: tue dec 27 22:52:55 cst 2016
begin run time: tue dec 27 22:52:57 cst 2016
end run time: tue dec 27 22:53:00 cst 2016

process finished with exit code 0

晚于当前基准时间

date = tue dec 27 22:37:00 cst 2016 nowtime = tue dec 27 22:36:06 cst 2016
begin run time: tue dec 27 22:37:00 cst 2016
end run time: tue dec 27 22:37:03 cst 2016
begin run time: tue dec 27 22:37:05 cst 2016
end run time: tue dec 27 22:37:08 cst 2016
begin run time: tue dec 27 22:37:10 cst 2016
end run time: tue dec 27 22:37:13 cst 2016

process finished with exit code 0

不延时的情况下,当早于基准时间时,时间差内的执行任务未补偿完时,下一次执行任务的时间参考的是上一次执行任务的结束时间;一旦补偿完毕(注意粗体时间点),下一次执行任务的时间参考的是上一次执行任务的开始时间;当晚于基准时间时,下一次执行任务的时间参考的是上一次执行任务的开始时间。

scheduleatfixedrate延时

使用示例

public class demo {
 private static timer timer = new timer();
 private static int runcount = 0;
 public static class mytask extends timertask {
 @override
 public void run() {
  try {
  system.out.println("begin run time: " + new date().tostring());
  thread.sleep(5000);
  system.out.println("end run time: " + new date().tostring());
  runcount++;
  if (runcount == 3) {
   timer.cancel();
  }
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 }
 }
 public static void main(string[] args) {
 try {
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestr = "2016-12-27 22:28:00";
  date date = sdf.parse(datestr);
  system.out.println("date = " + date.tostring() + " nowtime = " + new date().tostring());
  timer.scheduleatfixedrate(task, date, 3000);
 } catch (parseexception e) {
  e.printstacktrace();
 }
 }
}

执行结果

早于当前基准时间

date = tue dec 27 23:01:00 cst 2016 nowtime = tue dec 27 23:01:19 cst 2016
begin run time: tue dec 27 23:01:19 cst 2016
end run time: tue dec 27 23:01:24 cst 2016
begin run time: tue dec 27 23:01:24 cst 2016
end run time: tue dec 27 23:01:29 cst 2016
begin run time: tue dec 27 23:01:29 cst 2016
end run time: tue dec 27 23:01:34 cst 2016
begin run time: tue dec 27 23:01:34 cst 2016
end run time: tue dec 27 23:01:39 cst 2016

晚于当前基准时间

date = tue dec 27 22:28:00 cst 2016 nowtime = tue dec 27 22:27:55 cst 2016
begin run time: tue dec 27 22:28:00 cst 2016
end run time: tue dec 27 22:28:05 cst 2016
begin run time: tue dec 27 22:28:05 cst 2016
end run time: tue dec 27 22:28:10 cst 2016
begin run time: tue dec 27 22:28:10 cst 2016
end run time: tue dec 27 22:28:15 cst 2016

process finished with exit code 0

延时的情况下,即使是早于基准时间,由于延时效应,根本不可能补偿完毕时间差内的执行任务,故而在延时的情况下,下一次任务的执行时间都是参考上一次任务结束的时间来计算。

对比总结

执行任务不延时 执行任务延时
早于当前基准时间 schedule:下一次任务的执行时间参考的是上一次任务的开始时间来计算。 scheduleatfixedrate:当早于基准时间时,时间差内的执行任务未补偿完时,下一次执行任务的时间参考的是上一次任务的结束时间;一旦补偿完毕,下一次执行任务的时间参考上一次任务的开始时间来计算。 二者一样。下一次任务的执行时间都是参考上一次任务的结束时间来计算。
晚于当前基准时间 二者一样。下一次任务的执行时间参考的是上一次任务的开始时间来计算。 二者一样。下一次任务的执行时间都是参考上一次任务的结束时间来计算。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!