定时器Timer

Timer类的主要作用是设置任务计划,但是封装任务的类却是TimerTask类

 

 

一、如何设置计划任务

1、执行任务时间晚于当前时间-在未来执行的效果

 任务代码

 1 package chapter06;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTask extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("任务执行了,时间为:"+new Date());
12     }
13     
14 }

执行代码

 1 package chapter06;
 2 
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 
 7 public class Test1 {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         System.out.println("当前时间:"+new Date());
12         
13         //获取当前时间,并在当前时间的基础上加上10秒
14         Calendar calendarRef = Calendar.getInstance();
15         calendarRef.add(Calendar.SECOND, 10);
16         
17         // 当前延迟之后的Date
18         Date runDate = calendarRef.getTime();
19         
20         
21         MyTask task = new MyTask();//任务
22         // 设置任务计划
23         Timer timer = new Timer();
24         timer.schedule(task, runDate);
25     }
26 
27 }

 

2、在指定时间之后,按照指定时间间隔周期,无限循环执行某一个任务

 1 package chapter06;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTask extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("任务执行了,时间为:"+new Date());
12     }
13     
14 }
 1 package chapter06;
 2 
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 
 7 public class Test2 {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         System.out.println("当前时间为:"+new Date());
12         Calendar calendarRef = Calendar.getInstance();
13         
14         calendarRef.add(Calendar.SECOND, 10);
15         Date runDate = calendarRef.getTime();
16         System.out.println("计划时间为:"+runDate);
17         
18         MyTask task = new MyTask();
19         Timer timer = new Timer();
20         
21         timer.scheduleAtFixedRate(task, runDate, 4000);
22     }
23 
24 }

 

二、TimerTask类的cancel()方法和Timer类的cancel()方法

TimerTask的cancel()方法的作用是将自身凑够任务队列中进行清除

Timer类的cancel()方法的作用是将任务队列的任务全部清空

 

TimerTask类的calcel()方法例子

 

 1 package chapter07;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTaskA extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("A run time="+new Date());
12         this.cancel();
13         System.out.println("A任务自己移除了自己");
14     }
15     
16 }

 

 1 package chapter07;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTaskB extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("B run timer="+new Date());
12     }
13     
14 }
 1 package chapter07;
 2 
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 
 7 public class Test {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         System.out.println("当前时间:"+new Date());
12         
13         //获取当前时间,并在当前时间的基础上加上10秒
14         Calendar calendarRef = Calendar.getInstance();
15         calendarRef.add(Calendar.SECOND, 10);
16         
17         // 当前延迟之后的Date
18         Date runDate = calendarRef.getTime();
19         
20         
21         MyTaskA task1 = new MyTaskA();//任务A
22         MyTaskB task2 = new MyTaskB();//任务B
23         
24         // 设置任务计划
25         Timer timer = new Timer();
26         
27         timer.schedule(task1, runDate,4000);
28         timer.schedule(task2, runDate, 4000);
29     }
30 
31 }

 

 

Timer类的calcel()方法例子

 

 1 package chapter07;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTaskB extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("B run timer="+new Date());
12     }
13     
14 }

 

 1 package chapter07;
 2 
 3 import java.util.Date;
 4 import java.util.TimerTask;
 5 
 6 public class MyTaskC extends TimerTask {
 7 
 8     @Override
 9     public void run() {
10         // TODO Auto-generated method stub
11         System.out.println("C run timer="+new Date());
12     }
13     
14 }
 1 package chapter07;
 2 
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Timer;
 6 
 7 public class Test01 {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         System.out.println("当前时间:"+new Date());
12         
13         //获取当前时间,并在当前时间的基础上加上10秒
14         Calendar calendarRef = Calendar.getInstance();
15         calendarRef.add(Calendar.SECOND, 10);
16         
17         // 当前延迟之后的Date
18         Date runDate = calendarRef.getTime();
19         
20         
21         MyTaskB task1 = new MyTaskB();//任务B
22         MyTaskC task2 = new MyTaskC();//任务C
23         
24         // 设置任务计划
25         Timer timer = new Timer();
26         
27         timer.schedule(task1, runDate,4000);
28         timer.schedule(task2, runDate, 4000);
29         
30         timer.cancel();
31     }
32 
33 }

全部任务被清除,并且进程被销毁,按钮由红色变成灰色

 

posted @ 2019-01-06 11:13  林木声  阅读(333)  评论(0编辑  收藏  举报