当前位置: 移动技术网 > IT编程>开发语言>Java > Java多线程定时器Timer原理及实现

Java多线程定时器Timer原理及实现

2019年07月19日  | 移动技术网IT编程  | 我要评论

网易园林论坛,天津海魔方地址,恋母吧

前言

定时/计划功能在java应用的各个领域都使用得非常多,比方说web层面,可能一个项目要定时采集话单、定时更新某些缓存、定时清理一批不活跃用户等等。定时计划任务功能在java中主要使用的就是timer对象,它在内部使用多线程方式进行处理,所以它和多线程技术关联还是相当大的。那和threadlocal一样,还是先讲原理再讲使用,timer的实现原理不难,就简单扫一下就好了。

timer的schedule(timetask task, date time)的使用

该方法的作用是在执行的日期执行一次任务

1、执行任务的时间晚于当前时间:未来执行

private static timer timer = new timer();
static public class mytask extends timertask
{
  public void run()
  {
    system.out.println("运行了!时间为:" + new date());
  }
}
public static void main(string[] args) throws exception
{
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2015-10-6 12:14:00";
  date dateref = sdf.parse(datestring);
  system.out.println("字符串时间:" + dateref.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(task, dateref);
}

看一下运行效果:

字符串时间:2015-10-6 12:14:00 当前时间:2015-10-6 12:13:23
运行了!时间为:tue oct 06 12:14:00 cst 2015

执行时间和但前时间不一致,而是和dateref的时间一直,证明了未来执行。任务虽然执行完了,但进程没有销毁,控制台上的方框可以看到还是红色的,看下timer的源代码:

public timer() {
  this("timer-" + serialnumber());
}
public timer(string name) {
  thread.setname(name);
  thread.start();
}

所以,启动一个timer就是启动一个新线程,但是这个新线程并不是守护线程,所以它会一直运行。要运行完就让进程停止的话,设置timer为守护线程就好了,有专门的构造函数可以设置:

public timer(boolean isdaemon) {
  this("timer-" + serialnumber(), isdaemon);
}
public timer(string name, boolean isdaemon) {
  thread.setname(name);
  thread.setdaemon(isdaemon);
  thread.start();
}

2、计划时间早于当前时间:立即执行

如果执行任务的时间早于当前时间,那么立即执行task的任务:

private static timer timer = new timer();
static public class mytask extends timertask
{
  public void run()
  {
    system.out.println("运行了!时间为:" + new date());
  }
}
public static void main(string[] args) throws exception
{
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2014-10-6 12:14:00";
  date dateref = sdf.parse(datestring);
  system.out.println("字符串时间:" + dateref.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(task, dateref);
}

看一下运行效果:

字符串时间:2014-10-6 12:14:00 当前时间:2015-10-6 12:20:10
运行了!时间为:tue oct 06 12:20:10 cst 2015

执行时间和当前时间一致,证明了立即执行

3、多个timertask任务执行

timer中允许有多个任务:

private static timer timer = new timer();
static public class mytask extends timertask
{
  public void run()
  {
    system.out.println("运行了!时间为:" + new date());
  }
}
public static void main(string[] args) throws exception
{
  mytask task1 = new mytask();
  mytask task2 = new mytask();
  simpledateformat sdf1 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  simpledateformat sdf2 = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring1 = "2015-10-6 12:26:00";
  string datestring2 = "2015-10-6 12:27:00";
  date dateref1 = sdf1.parse(datestring1);
  date dateref2 = sdf2.parse(datestring2);
  system.out.println("字符串时间:" + dateref1.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  system.out.println("字符串时间:" + dateref2.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(task1, dateref1);
  timer.schedule(task2, dateref2);
}

看一下运行结果:

字符串时间:2015-10-612:26:00当前时间:2015-10-612:25:38

字符串时间:2015-10-612:27:00当前时间:2015-10-612:25:38

运行了!时间为:tueoct0612:26:00cst2015

运行了!时间为:tueoct0612:27:00cst2015

可以看到,运行时间和设置的时间一致,证明了未来可以执行多个任务。另外注意,task是以队列的方式一个一个被顺序执行的,所以执行的时间有可能和预期的时间不一致,因为前面的任务可能消耗过长,后面任务的运行时间也有可能被延迟。

代码就不写了,举个例子,任务1计划12:00:00被执行,任务2计划12:00:10被执行,结果任务1执行了30秒,那么任务2将在12:00:30被执行,因为task是被放入队列中的,因此必须一个一个顺序运行。

timer的schedule(timertasktask,datefirsttime,longperiod)

该方法的作用是在指定的日期之后,按指定的间隔周期性地无限循环地执行某一人物

1、计划时间晚于当前时间:未来执行

static public class mytask extends timertask
{
  public void run()
  {
    system.out.println("运行了!时间为:" + new date());
  }
}
public static void main(string[] args) throws exception
{
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2015-10-6 18:00:00";
  timer timer = new timer();
  date dateref = sdf.parse(datestring);
  system.out.println("字符串时间:" + dateref.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(task, dateref, 4000);
}

看一下运行结果:

字符串时间:2015-10-6 18:01:00 当前时间:2015-10-6 18:00:15
运行了!时间为:tue oct 06 18:01:00 cst 2015
运行了!时间为:tue oct 06 18:01:04 cst 2015
运行了!时间为:tue oct 06 18:01:08 cst 2015
运行了!时间为:tue oct 06 18:01:12 cst 2015
...

看到从设定的时间开始,每隔4秒打印一次,无限打印下去

2、计划时间早于当前时间:立即执行

static public class mytask extends timertask
{
  public void run()
  {
    system.out.println("运行了!时间为:" + new date());
  }
}
public static void main(string[] args) throws exception
{
  mytask task = new mytask();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2014-10-6 18:01:00";
  timer timer = new timer();
  date dateref = sdf.parse(datestring);
  system.out.println("字符串时间:" + dateref.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(task, dateref, 4000);
}

看一下运行结果:

字符串时间:2014-10-6 18:01:00 当前时间:2015-10-6 18:02:46
运行了!时间为:tue oct 06 18:02:46 cst 2015
运行了!时间为:tue oct 06 18:02:50 cst 2015
运行了!时间为:tue oct 06 18:02:54 cst 2015
运行了!时间为:tue oct 06 18:02:58 cst 2015
运行了!时间为:tue oct 06 18:03:02 cst 2015
...

看到运行时间比当前时间早,从当前时间开始,每隔4秒打印一次,无限循环下去

timertask的cancel()方法

timertask的cancel()方法的作用是将自身从任务队列中清除:

static public class mytaska extends timertask
{
  public void run()
  {
    system.out.println("a运行了!时间为:" + new date());
    this.cancel();
  }
}
  
static public class mytaskb extends timertask
{
  public void run()
  {
    system.out.println("b运行了!时间为:" + new date());
  }
}
  
public static void main(string[] args) throws exception
{
  mytaska taska = new mytaska();
  mytaskb taskb = new mytaskb();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2015-10-6 18:10: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);  
}

看一下运行结果:

字符串时间:2015-10-6 18:10:00 当前时间:2015-10-6 18:09:47
a运行了!时间为:tue oct 06 18:10:00 cst 2015
b运行了!时间为:tue oct 06 18:10:00 cst 2015
b运行了!时间为:tue oct 06 18:10:04 cst 2015
b运行了!时间为:tue oct 06 18:10:08 cst 2015
b运行了!时间为:tue oct 06 18:10:12 cst 2015
...

看到timetask的cancel()方法是将自身从任务队列中被移除,其他任务不受影响

timer的cancel()方法

把上面代码改动一下:

private static timer timer = new timer();
  
static public class mytaska extends timertask
{
  public void run()
  {
    system.out.println("a运行了!时间为:" + new date());
    timer.cancel();
  }
}
  
static public class mytaskb extends timertask
{
  public void run()
  {
    system.out.println("b运行了!时间为:" + new date());
  }
}
  
public static void main(string[] args) throws exception
{
  mytaska taska = new mytaska();
  mytaskb taskb = new mytaskb();
  simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss");
  string datestring = "2015-10-6 18:10:00";
  date dateref = sdf.parse(datestring);
  system.out.println("字符串时间:" + dateref.tolocalestring() + " 当前时间:" + new date().tolocalestring());
  timer.schedule(taska, dateref, 4000);
  timer.schedule(taskb, dateref, 4000);
}

看一下运行结果:

字符串时间:2015-10-618:10:00当前时间:2015-10-618:14:15

a运行了!时间为:tueoct0618:14:15cst2015

全部任务都被清除,并且进程被销毁。不过注意一下,cancel()方法未必一定会停止执行计划任务,可能正常执行,因为cancel()方法会尝试去获取queue锁,如果并没有获取到queue锁的话,timertask类中的任务继续执行也是完全有可能的

其他方法

再列举一些timer中的其他schedule的重载方法的作用,就不提供证明的代码了,可以自己尝试一下:

1、schedule(timertasktask,longdelay)

以当前时间为参考,在此时间基础上延迟指定的毫秒数后执行一次timertask任务

2、schedule(timertasktask,longdelay,longperiod)

以当前时间为参考,在此时间基础上延迟指定的毫秒数后,以period为循环周期,循环执行timertask任务

3、scheduleatfixedrate(timertasktask,datefirsttime,longperiod)

在延时的场景下,schedule方法和scheduleatfixedrate方法没有区别,它们的区别只是在非延时上。如果执行任务的时间没有被延时,对于schedule方法来说,下一次任务执行的时间参考的是上一次任务的开始时间来计算的;对于scheduleatfixedrate方法来说,下一次任务执行的时间参考的是上一次任务的结束时间来计算的

总结

以上就是本文关于java多线程定时器timer原理及实现的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

浅谈java多线程的优点及代码示例

java多线程之readwritelock读写分离的实现代码

如有不足之处,欢迎留言指出。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网