首页 技术 正文
技术 2022年11月15日
0 收藏 908 点赞 4,820 浏览 2973 个字

Java定时器Timer
在JDK库中,Timer类主要负责计划任务的功能,也就是在指定的时开始执行某一个任务。Timer类的主要作用就是设置计划任务,但封装任务的类却是TimerTask类,执行计划任务的代码要放入TimerTask的子类中,因为TimerTask是一个抽象类。下面通过实例说一说,如何实现指定时间执行任务以及实现指定周期执行任务。
在指定时间执行
方法schedule(TimerTask,Date time),该方法的作用是在指定的日期执行一次某一任务。
执行任务类MyTask

public class MyTask extends TimerTask{
@Override
public void run() {
System.out.println("任务执行了,时间为:" + new Date());
System.gc(); // 回收Timer
this.cancel(); //结束当前线程
}
}

场景1:执行任务晚于当前时间–延迟执行

public class Test {
public static void main(String[] args) {
System.out.println("当前时间为:" + new Date());
Calendar calen = Calendar.getInstance();
calen.add(Calendar.SECOND,5);
Date runDate = calen.getTime();
MyTask task = new MyTask();
Timer timer = new Timer();
timer.schedule(task,runDate);
}
}

执行结果:

Connected to the target VM, address: ‘127.0.0.1:63242’, transport: ‘socket’

当前时间为:Wed Nov 14 17:19:53 CST 2018任务执行了,时间为:Wed Nov 14 17:19:58 CST 2018Disconnected from the target VM, address: ‘127.0.0.1:63242’, transport: ‘socket’Process finished with exit code 0场景2:执行任务的时间早于当前时间–立即执行

public class Test1 {
public static void main(String[] args) {
System.out.println("当前时间为:" + new Date());
Calendar calen = Calendar.getInstance();
calen.add(Calendar.SECOND,-10);
Date runDate = calen.getTime();
MyTask task = new MyTask();
Timer timer = new Timer();
timer.schedule(task,runDate);
}
}

执行结果:Connected to the target VM, address: ‘127.0.0.1:63249’, transport: ‘socket’当前时间为:Wed Nov 14 17:20:58 CST 2018任务执行了,时间为:Wed Nov 14 17:20:58 CST 2018Disconnected from the target VM, address: ‘127.0.0.1:63249’, transport: ‘socket’Process finished with exit code 0场景3:Timer中允许有多个TimerTask

public class Test2 {
public static void main(String[] args) {
System.out.println("当前时间为:" + new Date());
Calendar calen1 = Calendar.getInstance();
calen1.add(Calendar.SECOND,5);
Date runDate1 = calen1.getTime();
MyTask task1 = new MyTask(); Calendar calen2 = Calendar.getInstance();
calen2.add(Calendar.SECOND,-10);
Date runDate2 = calen2.getTime();
MyTask task2 = new MyTask(); Timer timer = new Timer();
timer.schedule(task1,runDate1);
timer.schedule(task2,runDate2); }
}

执行结果:Connected to the target VM, address: ‘127.0.0.1:63292’, transport: ‘socket’当前时间为:Wed Nov 14 17:23:02 CST 2018任务执行了,时间为:Wed Nov 14 17:23:02 CST 2018任务执行了,时间为:Wed Nov 14 17:23:07 CST 2018Disconnected from the target VM, address: ‘127.0.0.1:63292’, transport: ‘socket’Process finished with exit code 0

周期性执行

方法schedule(TimerTask,Date firstTime,long period),改方法的作用是在指定的日期之后按指定的时间间隔周期,无线循环地执行某一任务。执行类:MyTask1

public class MyTask1 extends TimerTask{
@Override
public void run() {
System.out.println("任务执行了,时间为:" + new Date());
}
}

测试类:Test3

public class Test3 {
public static void main(String[] args) {
System.out.println("当前时间为:" + new Date());
Calendar calen = Calendar.getInstance();
calen.add(Calendar.SECOND,5);
Date runDate = calen.getTime();
MyTask1 task = new MyTask1();
Timer timer = new Timer();
timer.schedule(task,runDate,4000);
}
}

执行结果:Connected to the target VM, address: ‘127.0.0.1:63382’, transport: ‘socket’当前时间为:Wed Nov 14 17:35:36 CST 2018任务执行了,时间为:Wed Nov 14 17:35:41 CST 2018任务执行了,时间为:Wed Nov 14 17:35:45 CST 2018任务执行了,时间为:Wed Nov 14 17:35:49 CST 2018任务执行了,时间为:Wed Nov 14 17:35:53 CST 2018任务执行了,时间为:Wed Nov 14 17:35:57 CST 2018注意:1)TimerTask类中的cancel()方法的做事是将自身从任务队列中进行清除2)Timer类中的cancel()方法的作用是将任务队列中的全部任务进行清空。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,030
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,147
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859