Java -- timer problem, java -- Timer
Timer Problems
The timer is a basic component. Whether it is user space program development or kernel space program development, a timer is often needed as the basic component. The implementation of a timer requires the following four basic actions: adding a timer, canceling a timer, checking the timer, and executing the timer upon expiration.
Please design a timer and implement the following three basic actions. The function prototype has been provided. You can design the data structure and implementation using any programming language, and support a large number of timers as efficiently as possible:
// Add a Timer: perform the target operation after the specified interval.
// Input 1: Interval timer time, in ms
// Input 2: ExpiryAction target operation
// Return: timer Id
StartTimer (Interval, ExpiryAction)-> TimerId
// Cancel the timer
// Input: timer Id
StopTimer (TimerId)
// Timer check
// The system calls this function every 10 ms.
PerTickBookkeeping ()
If you don't talk much about it, go directly to the Code:
1) Timer. java:
1 import java. util. arrayList; 2 3 public class Timer {4 5 private long interval; // Timer time, In MS 6 private String expiryAction; // target operation 7 private int timerId; // Timer Id 8 private long waitTime; // Timer wait time 9 10 // constructor 11 public Timer () {12 this. waitTime = 0; 13} 14 15 // Add the timer 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 // cancel the Timer 24 public void StopTimer (int timerId, ArrayList <timer> timer) {25 Timer. remove (timerId); 26} 27 28 // The timer checks 29 public void PerTickBookkeeping () {30 if (this. interval> this. waitTime) 31 this. waitTime + = 10; 32 else {33 System. out. println ("timer" + 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, "I am the first Timer, wait 3 seconds ", 0); 13 Timer timer2 = new Timer (); 14 timer2.StartTimer (4000, "I'm the second Timer, wait 4 seconds", 1); 15 timerList. add (timer1); 16 timerList. add (timer2); 17 18 // public void run () {}19 new Thread () {20 @ Override21 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 block31 e. printStackTrace (); 32} 33} 34} 35 }. start (); 36 timer1.StopTimer (timer1.getTimerId (), timerList); 37 38} 39}
Thank you for your suggestions for improvement. Mr. Li will choose a better method to improve his program!
All rights reserved. Reprinting is allowed. Please indicate the source for reprinting. infringement is required!