最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java定時(shí)器問題實(shí)例解析

 更新時(shí)間:2017年04月20日 11:30:24   作者:小李編程助手  
這篇文章主要結(jié)合實(shí)例介紹了java當(dāng)中的定時(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)文章

最新評(píng)論

苍南县| 康定县| 河北省| 精河县| 鄢陵县| 祁连县| 柳江县| 社会| 麻江县| 祁门县| 宜川县| 汝阳县| 西乌珠穆沁旗| 陕西省| 霍城县| 内黄县| 巴青县| 越西县| 勐海县| 增城市| 乐陵市| 宁陕县| 信宜市| 赤水市| 常州市| 房产| 保定市| 连山| 柘荣县| 顺平县| 蕉岭县| 吉隆县| 四平市| 宁南县| 渝中区| 汝阳县| 武夷山市| 武胜县| 同仁县| 吉木萨尔县| 临汾市|