Home  >  Article  >  Java  >  Detailed explanation of code to implement timer function in Java

Detailed explanation of code to implement timer function in Java

Y2J
Y2JOriginal
2017-05-17 09:19:391978browse

This article mainly introduces some problems of timers in Java with examples. Friends in need can refer to it

Timer problems

Timers It is a basic basic component. Whether it is program development in user space or kernel space, timers are often needed as support as a basic component. The implementation of a timer needs to have the following four basic behaviors: adding timer, canceling timer, timer check, and expiration execution.

Please design a timer and implement the following three basic behaviors. Function The prototype has been 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 a timer: perform the target operation after a specific time interval

// Input 1: Interval timer time, Unit ms

// Input 2: ExpiryAction target operation

// Return: timer Id

StartTimer(Interval, ExpiryAction) -> TimerId

 //Cancel timer

 //Input: TimerId

 StopTimer(TimerId)

// Timer check

// The system will call this function every 10ms

PerTickBookkeeping()

Without further ado, let’s go straight to the code:

1) Timer.java:


##

import java.util.ArrayList;
public class Timer {
 private long interval; // 定时器时间,单位 ms
 private String expiryAction; // 目标操作
 private int timerId; // 定时器Id
 private long waitTime; // 定时器等待时间 
 // 构造函数
 public Timer(){
  this.waitTime = 0;
 } 
 // 添加定时器
 public int StartTimer(long interval, String expiryAction, int id){
  this.interval = interval;
  this.expiryAction = expiryAction;
  this.timerId = id;
  return timerId;
 } 
 // 取消定时器
 public void StopTimer(int timerId, ArrayList<Timer> timer){
  timer.remove(timerId);
 }
 // 定时器检查
 public void PerTickBookkeeping(){
  if (this.interval > this.waitTime)
   this.waitTime += 10;
  else{
   System.out.println("定时器"+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, "我是第一个定时器,等待3秒", 0);
  Timer timer2 = new Timer();
  timer2.StartTimer(4000, "我是第二个定时器,等待4秒", 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);
 }
}

【Related recommendations】

1.

Special recommendation "php Programmer Toolbox" V0.1 version download

2.

Java free video tutorial

3.

Comprehensive analysis of Java annotations

The above is the detailed content of Detailed explanation of code to implement timer function in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn