欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java定時器問題實例解析

 更新時間:2017年04月20日 11:30:24   作者:小李編程助手  
這篇文章主要結(jié)合實例介紹了java當(dāng)中的定時器的一些問題,有需要的朋友可以參考一下

定時器問題

  定時器屬于基本的基礎(chǔ)組件,不管是用戶空間的程序開發(fā),還是內(nèi)核空間的程序開發(fā),很多時候都需要有定時器作為基礎(chǔ)組件的支持。一個定時器的實現(xiàn)需要具備以下四種基本行為:添加定時器、取消定時器、定時器檢查、到期執(zhí)行。

  請設(shè)計一個定時器并實現(xiàn)以下三種基本行為,函數(shù)原型已給出,可使用任意編程語言設(shè)計數(shù)據(jù)結(jié)構(gòu)及實現(xiàn),并盡可能高效地支持大量定時器:  

       // 添加定時器:經(jīng)過特定時間間隔后執(zhí)行目標(biāo)操作

  // 輸入 1:Interval 定時器時間,單位ms

  // 輸入 2:ExpiryAction 目標(biāo)操作

  // 返回:定時器 Id

  StartTimer(Interval, ExpiryAction) -> TimerId

  // 取消定時器

  // 輸入:定時器 Id

  StopTimer(TimerId)

  // 定時器檢查

  // 系統(tǒng)每隔 10ms 會調(diào)用一次該函數(shù)

  PerTickBookkeeping()

  話不多說,直接上代碼:

  1)Timer.java:

import java.util.ArrayList;
public class Timer {
 private long interval; // 定時器時間,單位 ms
 private String expiryAction; // 目標(biāo)操作
 private int timerId; // 定時器Id
 private long waitTime; // 定時器等待時間 
 // 構(gòu)造函數(shù)
 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);
 }
}

希望本篇文章可以幫助到您

相關(guān)文章

  • Java經(jīng)典面試題匯總:JVM

    Java經(jīng)典面試題匯總:JVM

    本篇總結(jié)的是JVM相關(guān)的面試題,后續(xù)會持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯誤還望大家多多包涵,不吝賜教,謝謝
    2021-07-07
  • Spring?AOP?創(chuàng)建代理對象詳情

    Spring?AOP?創(chuàng)建代理對象詳情

    這篇文章介紹了Spring?AOP?創(chuàng)建代理對象詳情,主要介紹AOP?創(chuàng)建代理對象和上下文相關(guān)的內(nèi)容,下文分享具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • idea2020.3測試評價及感受

    idea2020.3測試評價及感受

    idea2020.3版本這次變化最大的也就是 UI了完全拋棄了之前一直使用的模板更改成了新的樣式,感興趣的朋友快來下載體驗下吧
    2020-10-10
  • java批量解析微信dat文件

    java批量解析微信dat文件

    這篇文章主要為大家詳細(xì)介紹了java批量解析微信dat文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java MAVEN 工程pom配置報錯解決方案

    Java MAVEN 工程pom配置報錯解決方案

    這篇文章主要介紹了Java MAVEN 工程pom配置報錯解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解

    Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解

    今天小編就為大家分享一篇關(guān)于Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Java Web程序中利用Spring框架返回JSON格式的日期

    Java Web程序中利用Spring框架返回JSON格式的日期

    這里我們來介紹一下Java Web程序中利用Spring框架返回JSON格式的日期的方法,前提注意使用@DatetimeFormat時要引入一個類庫joda-time-版本.jar,否則會無法訪問相應(yīng)路徑
    2016-05-05
  • mybatis-sqlserver批量新增返回id方式

    mybatis-sqlserver批量新增返回id方式

    這篇文章主要介紹了mybatis-sqlserver批量新增返回id方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • springboot集成nacos實現(xiàn)自動刷新的示例代碼

    springboot集成nacos實現(xiàn)自動刷新的示例代碼

    研究nacos時發(fā)現(xiàn),springboot版本可使用@NacosValue實現(xiàn)配置的自動刷新,本文主要介紹了springboot集成nacos實現(xiàn)自動刷新的示例代碼,感興趣的可以了解一下
    2023-11-11
  • JDK動態(tài)代理接口和接口實現(xiàn)類深入詳解

    JDK動態(tài)代理接口和接口實現(xiàn)類深入詳解

    這篇文章主要介紹了JDK動態(tài)代理接口和接口實現(xiàn)類,JDK動態(tài)代理是代理模式的一種實現(xiàn)方式,因為它是基于接口來做代理的,所以也常被稱為接口代理,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評論