Timer类的使用

原创
2017/01/09 14:11
阅读数 39

quartz框架的使用

Timer类的使用

功能:设置计划任务,但是封装任务的类却是TImerTask类,执行计划任务的代码要放入TimeTask的子类中;

TimerTask是一个抽象类

 

1例子:

schedule(Timer timer,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 (Exception e) {

                                     e.printStackTrace();

                            }

                   }

         }

}

结果:任务执行完成,但是进程没有销毁

内部实现:

public Timer(String name){

thread.setName(name);

thread.start();

}

创建一个Timer就是启动一个新的线程,这个新启动的线程不是守护线程,它一直运行着。

 

2例子:守护线程

public class RunTimerisDaemon {

         private static Timer timer = new Timer(true);



         static public class MyTask extends TimerTask {



                   @Override

                   public void run() {

                            System.out.println("yunxingle,时间为" + 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 (Exception e) {

                                     e.printStackTrace();

                            }

                   }

         }

}

结果:程序运行后迅速结束当前进程,TimerTask中的任务不再被运行,因为进程已经结束了

 

 

3例子:多个TimerTask任务及延时的测试

 


public class Run2 {

         private static Timer timer = new Timer();



         static public class MyTask1 extends TimerTask {

                   @Override

                   public void run() {

                            System.out.println("运行le,时间为:" + new Date());

                   }

         }



         static public class MyTask2 extends TimerTask {

                   @Override

                   public void run() {

                            System.out.println("运行了,shijiawei" + new Date());

                   }

         }



         public static void main(String[] args) throws ParseException {

                   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());

                   System.out.println("字符串2时间:" + dateRef2.toLocaleString() + "当前时间:" + new Date());

                   timer.schedule(task1, dateRef1);

                   timer.schedule(task2, dateRef2);

         }

}

结果:TimerTask是以一个队列的方式一个一个被顺序执行的。

 

 

schedule(TimeTask task,Date firstTime,long period)测试

功能:在指定的日期之后,按指定的间隔周期性地无限循环的执行某个任务

例子:

public class Run {

         static public class MyTask extends TimerTask {

                   @Override

                   public void run() {

                            System.out.println("yunxingle:shijianwei" + new Date());

                   }

         }



         public static void main(String[] args) throws ParseException {

                   MyTask task = new MyTask();



                   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                   String dateString = "2014-10-12 10:39:00";



                   Date dateRef = sdf.parse(dateString);

                   Timer timer = new Timer();

                   System.out.println("字符串1时间:" + dateRef.toLocaleString() + "当前时间:" + new Date());

                   timer.schedule(task, dateRef, 2000);

         }

}

结果:每个2秒运行一次TimerTask任务,并且是无期限地重复执行。

 

TimerTask类的cancel()方法

功能:将自身从任务队列中清除

this.cancel()只运行一次

 

Timer类的Cancel()方法

功能:和TimerTask的cancel清除自身不同,这里的cancel作用是将任务队列中全部任务清空。

Timer类的cancel()方法有时并没有争抢到queue锁,所以TimerTask类中的任务继续正常执行

 

方法schedule(TimerTask task,long delay)的测试

功能:以执行schedule(TimerTask task,long delay)方法当前的时间为参考时间,在此基础上延迟指定的毫秒数后执行一次TImerTask任务。

 

例子:

public class RunTimer {

         static public class MyTask extends TimerTask {



                   @Override

                   public void run() {

                            System.out.println("run ing time:" + 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);

         }

}

结果:当前时间:2017-1-9 13:46:02

run ing time:Mon Jan 09 13:46:06 CST 2017

 

 

方法schedule(TimeTask task,long delay,long period)

功能:以执行schedule(TimerTask task,long delay,long period)的当前时间为参考时间,在此时间基础上延迟指定的毫秒数,再以某个间隔时间无限次数的执行某个任务。

方法scheduleAtFixedRate(TimerTask task,Date firstTime,long period)

方法任务延时;方法任务不延时;开始执行的时间不同;

 

 

展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部