码迷,mamicode.com
首页 > 编程语言 > 详细

Java--定时器问题

时间:2017-04-09 10:53:11      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:方法   支持   interrupt   rem   16px   操作   void   问题   generate   

定时器问题

  定时器属于基本的基础组件,不管是用户空间的程序开发,还是内核空间的程序开发,很多时候都需要有定时器作为基础组件的支持。一个定时器的实现需要具备以下四种基本行为:添加定时器、取消定时器、定时器检查、到期执行。
  请设计一个定时器并实现以下三种基本行为,函数原型已给出,可使用任意编程语言设计数据结构及实现,并尽可能高效地支持大量定时器:
  // 添加定时器:经过特定时间间隔后执行目标操作
  // 输入 1:Interval 定时器时间,单位ms
  // 输入 2:ExpiryAction 目标操作
  // 返回:定时器 Id
  StartTimer(Interval, ExpiryAction) -> TimerId
  // 取消定时器
  // 输入:定时器 Id
  StopTimer(TimerId)
  // 定时器检查
  // 系统每隔 10ms 会调用一次该函数
  PerTickBookkeeping()

  话不多说,直接上代码:

  1)Timer.java:

 1 import java.util.ArrayList;
 2 
 3 public class Timer {
 4 
 5     private long interval;  // 定时器时间,单位 ms
 6     private String expiryAction;  // 目标操作
 7     private int timerId;  // 定时器Id
 8     private long waitTime;  // 定时器等待时间
 9     
10     // 构造函数
11     public Timer(){
12         this.waitTime = 0;
13     }
14     
15     // 添加定时器
16     public int StartTimer(long interval, String expiryAction, int id){
17         this.interval = interval;
18         this.expiryAction = expiryAction;
19         this.timerId = id;
20         return timerId;
21     }
22     
23     // 取消定时器
24     public void StopTimer(int timerId, ArrayList<Timer> timer){
25         timer.remove(timerId);
26     }
27     
28     // 定时器检查
29     public void PerTickBookkeeping(){
30         if (this.interval > this.waitTime)
31             this.waitTime += 10;
32         else{
33             System.out.println("定时器"+this.timerId+":"+this.expiryAction);
34             this.waitTime = 0;
35         }
36     }
37     
38     
39     public long getInterval() {
40         return interval;
41     }
42     public void setInterval(long interval) {
43         this.interval = interval;
44     }
45     public String getExpiryAction() {
46         return expiryAction;
47     }
48     public void setExpiryAction(String expiryAction) {
49         this.expiryAction = expiryAction;
50     }
51     public int getTimerId() {
52         return timerId;
53     }
54     public void setTimerId(int timerId) {
55         this.timerId = timerId;
56     }
57     public long getWaitTime() {
58         return waitTime;
59     }
60     public void setWaitTime(long waitTime) {
61         this.waitTime = waitTime;
62     }
63 }

  2)DoTimer.java:

 1 import java.util.ArrayList;
 2 import java.util.Iterator;
 3 
 4 public class DoTimer extends Thread {
 5 
 6     private static ArrayList<Timer> timerList;
 7     
 8     public static void main(String[] args){
 9         
10         timerList = new ArrayList<Timer>();
11         Timer timer1 = new Timer();
12         timer1.StartTimer(3000, "我是第一个定时器,等待3秒", 0);
13         Timer timer2 = new Timer();
14         timer2.StartTimer(4000, "我是第二个定时器,等待4秒", 1);
15         timerList.add(timer1);
16         timerList.add(timer2);
17         
18         //public void run(){}
19         new Thread(){
20             @Override
21             public void run() {
22                 while(true){
23                     Iterator<Timer> it = timerList.iterator();
24                     while(it.hasNext()){
25                         it.next().PerTickBookkeeping();
26                     }
27                     try {
28                         sleep(10);
29                     } catch (InterruptedException e) {
30                         // TODO Auto-generated catch block
31                         e.printStackTrace();
32                     }
33                 }
34             }
35         }.start();
36         timer1.StopTimer(timer1.getTimerId(), timerList);
37         
38     }
39 }

  感谢各位提出改进意见,小李会选择更好的方法,改进自己的程序!

  

  版权所有,允许转载,转载请注明出处,侵权必究!

Java--定时器问题

标签:方法   支持   interrupt   rem   16px   操作   void   问题   generate   

原文地址:http://www.cnblogs.com/xiaoli-home/p/6683772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!