Java timer problem instance analysis

  • 2020-07-21 08:08:20
  • OfStack

Timer problem

Timer belongs to the basic components, whether it is user space program development, or kernel space program development, many times need timer as the support of the basic components. The implementation of a timer needs to have the following four basic behaviors: add the timer, cancel the timer, check the timer, and expire the execution.

Please design a timer and implement the following three basic behaviors. The function prototype is given. You can use any programming language to design the data structure and implementation, and support a large number of timers as efficiently as possible:

// Add timer: Performs target action after a specified time interval

Input 1: Interval timer time, unit 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 10ms

PerTickBookkeeping ()

Without further ado, code directly:

1) Timer. java:


import java.util.ArrayList;
public class Timer {
 private long interval; //  Timer time, units  ms
 private String expiryAction; //  Target operating 
 private int timerId; //  The timer Id
 private long waitTime; //  Timer wait time  
 //  The constructor 
 public Timer(){
  this.waitTime = 0;
 } 
 //  Add timer 
 public int StartTimer(long interval, String expiryAction, int id){
  this.interval = interval;
  this.expiryAction = expiryAction;
  this.timerId = id;
  return timerId;
 } 
 //  Cancel timer 
 public void StopTimer(int timerId, ArrayList<Timer> timer){
  timer.remove(timerId);
 }
 //  Timer check 
 public void PerTickBookkeeping(){
  if (this.interval > this.waitTime)
   this.waitTime += 10;
  else{
   System.out.println(" The timer "+this.timerId+":"+this.expiryAction);
   this.waitTime = 0;
  }
 }
 public long getInterval() {
  return interval;
 }
 public void setInterval(long interval) {
  this.interval = interval;
 }
 public String getExpiryAction() {
  return expiryAction;
 }
 public void setExpiryAction(String expiryAction) {
  this.expiryAction = expiryAction;
 }
 public int getTimerId() {
  return timerId;
 }
 public void setTimerId(int timerId) {
  this.timerId = timerId;
 }
 public long getWaitTime() {
  return waitTime;
 }
 public void setWaitTime(long waitTime) {
  this.waitTime = waitTime;
 }
}

2) DoTimer. java:


import java.util.ArrayList;
import java.util.Iterator;
public class DoTimer extends Thread {
 private static ArrayList<Timer> timerList;
 public static void main(String[] args){
  timerList = new ArrayList<Timer>();
  Timer timer1 = new Timer();
  timer1.StartTimer(3000, " I am the first 1 A timer. Wait 3 seconds ", 0);
  Timer timer2 = new Timer();
  timer2.StartTimer(4000, " I am the first 2 A timer. Wait 4 seconds ", 1);
  timerList.add(timer1);
  timerList.add(timer2);  
  //public void run(){}
  new Thread(){
   @Override
   public void run() {
    while(true){
     Iterator<Timer> it = timerList.iterator();
     while(it.hasNext()){
      it.next().PerTickBookkeeping();
     }
     try {
      sleep(10);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }.start();
  timer1.StopTimer(timer1.getTimerId(), timerList);
 }
}

I hope this article has helped you


Related articles: