Java定時(shí)器問題實(shí)例解析
定時(shí)器問題
定時(shí)器屬于基本的基礎(chǔ)組件,不管是用戶空間的程序開發(fā),還是內(nèi)核空間的程序開發(fā),很多時(shí)候都需要有定時(shí)器作為基礎(chǔ)組件的支持。一個(gè)定時(shí)器的實(shí)現(xiàn)需要具備以下四種基本行為:添加定時(shí)器、取消定時(shí)器、定時(shí)器檢查、到期執(zhí)行。
請(qǐng)?jiān)O(shè)計(jì)一個(gè)定時(shí)器并實(shí)現(xiàn)以下三種基本行為,函數(shù)原型已給出,可使用任意編程語言設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)及實(shí)現(xiàn),并盡可能高效地支持大量定時(shí)器:
// 添加定時(shí)器:經(jīng)過特定時(shí)間間隔后執(zhí)行目標(biāo)操作
// 輸入 1:Interval 定時(shí)器時(shí)間,單位ms
// 輸入 2:ExpiryAction 目標(biāo)操作
// 返回:定時(shí)器 Id
StartTimer(Interval, ExpiryAction) -> TimerId
// 取消定時(shí)器
// 輸入:定時(shí)器 Id
StopTimer(TimerId)
// 定時(shí)器檢查
// 系統(tǒng)每隔 10ms 會(huì)調(diào)用一次該函數(shù)
PerTickBookkeeping()
話不多說,直接上代碼:
1)Timer.java:
import java.util.ArrayList;
public class Timer {
private long interval; // 定時(shí)器時(shí)間,單位 ms
private String expiryAction; // 目標(biāo)操作
private int timerId; // 定時(shí)器Id
private long waitTime; // 定時(shí)器等待時(shí)間
// 構(gòu)造函數(shù)
public Timer(){
this.waitTime = 0;
}
// 添加定時(shí)器
public int StartTimer(long interval, String expiryAction, int id){
this.interval = interval;
this.expiryAction = expiryAction;
this.timerId = id;
return timerId;
}
// 取消定時(shí)器
public void StopTimer(int timerId, ArrayList<Timer> timer){
timer.remove(timerId);
}
// 定時(shí)器檢查
public void PerTickBookkeeping(){
if (this.interval > this.waitTime)
this.waitTime += 10;
else{
System.out.println("定時(shí)器"+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, "我是第一個(gè)定時(shí)器,等待3秒", 0);
Timer timer2 = new Timer();
timer2.StartTimer(4000, "我是第二個(gè)定時(shí)器,等待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);
}
}
希望本篇文章可以幫助到您
相關(guān)文章
Spring?AOP?創(chuàng)建代理對(duì)象詳情
這篇文章介紹了Spring?AOP?創(chuàng)建代理對(duì)象詳情,主要介紹AOP?創(chuàng)建代理對(duì)象和上下文相關(guān)的內(nèi)容,下文分享具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
idea2020.3測(cè)試評(píng)價(jià)及感受
idea2020.3版本這次變化最大的也就是 UI了完全拋棄了之前一直使用的模板更改成了新的樣式,感興趣的朋友快來下載體驗(yàn)下吧2020-10-10
Java MAVEN 工程pom配置報(bào)錯(cuò)解決方案
這篇文章主要介紹了Java MAVEN 工程pom配置報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java Web程序中利用Spring框架返回JSON格式的日期
這里我們來介紹一下Java Web程序中利用Spring框架返回JSON格式的日期的方法,前提注意使用@DatetimeFormat時(shí)要引入一個(gè)類庫joda-time-版本.jar,否則會(huì)無法訪問相應(yīng)路徑2016-05-05
springboot集成nacos實(shí)現(xiàn)自動(dòng)刷新的示例代碼
研究nacos時(shí)發(fā)現(xiàn),springboot版本可使用@NacosValue實(shí)現(xiàn)配置的自動(dòng)刷新,本文主要介紹了springboot集成nacos實(shí)現(xiàn)自動(dòng)刷新的示例代碼,感興趣的可以了解一下2023-11-11
JDK動(dòng)態(tài)代理接口和接口實(shí)現(xiàn)類深入詳解
這篇文章主要介紹了JDK動(dòng)態(tài)代理接口和接口實(shí)現(xiàn)類,JDK動(dòng)態(tài)代理是代理模式的一種實(shí)現(xiàn)方式,因?yàn)樗腔诮涌趤碜龃淼?所以也常被稱為接口代理,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06

