jdk中的定时器

首先看一下jdk自带定时器:

  一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。与每个 Timer 对象相对应的是单个后台线程,用于顺序地执行所有计时器任务。计时器任务应该迅速完成。如果完成某个计时器任务的时间太长,那么它会“独占”计时器的任务执行线程。因此,这就可能延迟后续任务的执行,而这些任务就可能“堆在一起”,并且在上述不友好的任务最终完成时才能够被快速连续地执行。

schedule(TimerTask task,long delay) 安排在指定延迟后执行指定的任务。
schedule(TimerTask task,Date time) 安排在指定的时间执行指定的任务。如果此时间已过去,则安排立即执行该任务。
schedule(TimerTask task, long delay, long period) 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟
schedule(TimerTask task,Date firstTime,long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。如果由于任何原因(如垃圾回收或其他后台活动)而延迟了某次执行,则后续执行也将被延迟。

 1 package test;
 2
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.Timer;
 7 import java.util.TimerTask;
 8
 9 /**
10  * jdk自带定时器
11  *
12  * @author LIUTIE
13  *
14  */
15 public class JDKTimer {
16
17
18     public static void main(String[] args) throws ParseException {
19         //日期格式工具
20         final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
21
22         Timer timer = new Timer();
23         // 10s后执行定时器,仅执行一次
24         System.out.print(sdf.format(new Date()));
25         System.out.println("the timer one will be executed after 10 seconds...");
26         long milliseconds = 10 * 1000;
27         timer.schedule(new TimerTask() {
28
29             @Override
30             public void run() {
31                 System.out.print(sdf.format(new Date()));
32                 System.out.println("the timer one has finished execution");
33             }
34         }, milliseconds);
35
36         //12秒后执行定时器,每1s执行一次
37         System.out.print(sdf.format(new Date()));
38         System.out.println("the timer two will be executed after 12 seconds...");
39         //启动后延迟时间
40         long afterSs = 12 * 1000;
41         //执行周期
42         long intervalSs1 = 1 * 1000;
43         timer.schedule(new TimerTask() {
44             // 执行计数器
45             int i = 0;
46
47             @Override
48             public void run() {
49                 System.out.print(sdf.format(new Date()));
50                 System.out.println("the timer two has execution " + (++i) + " timers");
51                 // 执行10次后关闭定时器
52                 if (i == 10) {
53                     this.cancel();
54                 }
55             }
56         }, afterSs, intervalSs1);
57
58
59         // 指定时间执行定时器,仅执行一次
60         System.out.print(sdf.format(new Date()));
61         System.out.println("the timer three will be executed at 2017-06-27 21:47:00...");
62         Date date = sdf.parse("2017-06-27 21:47:00");
63         timer.schedule(new TimerTask() {
64
65             @Override
66             public void run() {
67                 System.out.print(sdf.format(new Date()));
68                 System.out.println("the timer three has finished execution");
69             }
70         }, date);
71
72         // 从指定时间开始周期性执行
73         System.out.print(sdf.format(new Date()));
74         System.out.println("the timer four will be executed at 2017-06-27 21:48:00...");
75         // 执行间隔周期
76         long intervalSs = 1 * 1000;
77         // 开始执行时间
78         Date beginTime = sdf.parse("2017-06-27 21:48:00");
79         timer.schedule(new TimerTask() {
80             // 执行计数器
81             int i = 0;
82
83             @Override
84             public void run() {
85                 System.out.print(sdf.format(new Date()));
86                 System.out.println("the timer four has execution " + (++i) + " timers");
87                 // 执行10次后关闭定时器
88                 if (i == 10) {
89                     this.cancel();
90                 }
91             }
92         }, beginTime, intervalSs);
93     }
94
95 }

执行结果

2017-06-27 21:46:24the timer one will be executed after 10 seconds...
2017-06-27 21:46:24the timer two will be executed after 12 seconds...
2017-06-27 21:46:24the timer three will be executed at 2017-06-27 21:47:00...
2017-06-27 21:46:24the timer four will be executed at 2017-06-27 21:48:00...
2017-06-27 21:46:34the timer one has finished execution
2017-06-27 21:46:36the timer two has execution 1 timers
2017-06-27 21:46:37the timer two has execution 2 timers
2017-06-27 21:46:38the timer two has execution 3 timers
2017-06-27 21:46:39the timer two has execution 4 timers
2017-06-27 21:46:40the timer two has execution 5 timers
2017-06-27 21:46:41the timer two has execution 6 timers
2017-06-27 21:46:42the timer two has execution 7 timers
2017-06-27 21:46:43the timer two has execution 8 timers
2017-06-27 21:46:44the timer two has execution 9 timers
2017-06-27 21:46:45the timer two has execution 10 timers
2017-06-27 21:47:00the timer three has finished execution
2017-06-27 21:48:00the timer four has execution 1 timers
2017-06-27 21:48:01the timer four has execution 2 timers
2017-06-27 21:48:02the timer four has execution 3 timers
2017-06-27 21:48:03the timer four has execution 4 timers
2017-06-27 21:48:04the timer four has execution 5 timers
2017-06-27 21:48:05the timer four has execution 6 timers
2017-06-27 21:48:06the timer four has execution 7 timers
2017-06-27 21:48:07the timer four has execution 8 timers
2017-06-27 21:48:08the timer four has execution 9 timers
2017-06-27 21:48:09the timer four has execution 10 timers

时间: 2024-08-09 14:36:55

jdk中的定时器的相关文章

JDK中的Timer和TimerTask详解

目录结构: Timer和TimerTask 一个Timer调度的例子 如何终止Timer线程 关于cancle方式终止线程 反复执行一个任务 schedule VS. scheduleAtFixedRate 一些注意点 1. Timer和TimerTask Timer是jdk中提供的一个定时器工具,使用的时候会在主线程之外起一个单独的线程执行指定的计划任务,可以指定执行一次或者反复执行多次. TimerTask是一个实现了Runnable接口的抽象类,代表一个可以被Timer执行的任务. 2.

Android中实现定时器的3中方法

在Android开发中,定时器一般有以下3种实现方法: 一.采用Handler与线程的sleep(long)方法: 二.采用Handler的postDelayed(Runnable, long)方法: 三.采用Handler与timer及TimerTask结合的方法: 一.采用Handle与线程的sleep(long)方法 Handler主要用来处理接受到的消息.这只是最主要的方法,当然Handler里还有其他的方法供实现,有兴趣的可以去查API,这里不过多解释. 1. 定义一个Handler类

MFC的DLL中实现定时器功能

方法一:创建一个线程, 反复读系统时间不就可以了? 如果定时要求不严,用Sleep就可以了.DWORD WINAPI TimerThread(LPVOID pamaram) { UINT oldTickCount, newTickCount; oldTickCount = GetTickCount(); //获取的是毫秒数 while(TRUE) { while(TRUE) { newTickCount = GetTickCount(); // 获取的是毫秒数 if(newTickCount -

jdk 中Runtime之单例模式 学习

这段代码是我从源码中截取的,大家很容易看到currentRuntime是一个静态变量,getRunTime对应的就是getInstacne.不是说这种方法不好吗? 1 public class Runtime { 2 45 private static Runtime currentRuntime = new Runtime(); 3 4 5 Returns the runtime object associated with the current Java application. Most

WebService之JDK中wsimport命令

1.编写WebService类,使用@WebService注解 package test; import javax.jws.WebService; @WebService public class HelloServiceImpl{ public String say(String name) { return "Hello "+name; } } WebService类 2.使用main方法发布WebService package test; import javax.xml.ws

Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)

在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度 1.QObject类的定时器 QObject是所有Qt对象的基类,它提供了一个基本的定时器.通过QObject::startTimer(),可以把一个一毫秒为单位的时间间隔作为参数来开始定时器,这个函数返回一个唯一的整数定时器的标识符.这个定时器开始就会在每一个时间间隔"触发",直到明确的使用这个定时器的标识符来调用QObjec

Jdk中的设计模式

转自:http://blog.csdn.net/gtuu0123/article/details/6114197 本文主要是归纳了JDK中所包含的设计模式,包括作用和其设计类图.首先来个总结,具体的某个模式可以一个一个慢慢写,希望能对研究JDK和设计模式有所帮助. 一.设计模式是什么(1)反复出现问题的解决方案(2)增强软件的灵活性(3)适应软件不断变化 二.学习JDK中设计模式的好处(1)借鉴优秀代码的设计,有助于提高代码设计能力(2)JDK的设计中体现了大多数设计模式,是学习设计模式的较好的

Java中的定时器

注意:内容来自网络他人文章,特此声明 一.Java中的定时器 在JAVA中实现定时器功能要用的2个重要类是 Timer类:定时器类,需要一个TimerTask类的实例作为参数: TimerTask:定时器任务类,定时器要执行的任务在该类的run方法中定义. 二.实现定时器的两种写法 1.普通实现(相对于通过内部类来实现而言) 1 //先写一个类 2 public class TimeTest { 3 public static void main(String[] args) { 4 5 Tim

第十四篇:在SOUI中使用定时器

前言 定时器是win32编程中常用的制作动画效果的手段.在Win32编程中,可以使用::SetTimer来创建定时器,定时器消息会被会发到调用SetTimer时指定的HWND. 在SOUI中一般来说只有一个宿主窗口有HWND,所有的SWindow都属于一个宿主窗口,如此一来直接使用::SetTimer创建的定时器就难以直接分发到SWindow对象了. 在SOUI的控件中使用定时器 为了能够方便的在SWindow中使用定时器,在SOUI系统中,我们通过将定时器ID(共32位)按位进行分解: cla