2012-10-23 32 views
7
定時器

所以我試圖找出如何創建一個定時器,我碰到這樣的: using ScheduledExecutorService to start and stop timer創建具有ScheduledExecutorService的

他們的例子似乎工作得很好。我只是想知道如果我正確地利用這一點:

public class TimerTest 
{ 
    private ScheduledExecutorService es = null; 
    private boolean timeIsUp = false; 
    private ScheduledFuture futureHandler = null; 
    private TimeKeeper timeKeeper = null; 
    private String subject = ""; 
    private int siteNo; 
    private long time; 
    private boolean stop; 




public void endTimer() 
{ 
    System.out.println("we should shutdown everything here"); 
    es.shutdownNow(); 
} 

public boolean stopTimer() 
{ 

    if (timeKeeper != null) 
    { 
     timeKeeper.deactivate(); 
    } 
    futureHandler.cancel(true); 
return true; 

} 
public boolean isTimeup() 
{ 
    return timeKeeper.isTimeUp(); 
} 
public void startTimer(long mseconds, String subj, int sNo) 
{ 
    subject = subj; 
    siteNo = sNo; 
    time = mseconds; 
    timeKeeper = new TimeKeeper(); 
    callScheduler(mseconds); 

} 

public boolean isTimerRunning() 
{ 
    return (es.isShutdown() || es == null); 

} 
public void resetTimer(long t) 
{ 
    stopTimer(); 
    callScheduler(t); 
} 

public void resetTimer() 
{ 

    resetTimer(time); 
} 

private void callScheduler(long mseconds) 
{ 
    if (es == null) 
     es = Executors.newScheduledThreadPool(3); 
    timeKeeper = new TimeKeeper(); 
    futureHandler = es.schedule(timeKeeper, mseconds, TimeUnit.MILLISECONDS); 

} 


private class TimeKeeper implements Runnable { 
    //volatile for thread-safety 

    private volatile boolean isActive = true; 
    private volatile boolean isTimeUp = false; 
    public void run() { 
     if (isActive){ 
      callAlert(); 
      isTimeUp = true; 
     } 
    } 
    public void deactivate(){ 
     isActive = false; 
    } 

    public boolean isTimeUp() 
    { 
     return isTimeUp; 
    } 
    private void callAlert() 
    { 
     System.out.println("you are in the callAlert method"); 
    } 
    } 

} 

這裏是測試:

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    long pastTime = System.currentTimeMillis(); 
    TimerTest timer = new TimerTest(); 
    timer.startTimer(15000, "bh", 1); 
    long time; 
    int count =0; 
    boolean stop = false; 
    while(true) 
    { 

     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     time = System.currentTimeMillis() - pastTime; 

     if (time > 3000) 
     { 
      if (!stop){ 
       System.out.println("we are reseting the timer"); 
       timer.resetTimer(4000); 

       timer.stopTimer(); 
       try { 
        Thread.sleep(3995); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       break; 

      } 
      stop = true; 


     } 
     if (timer.isTimeup()) 
     { 
      System.out.println("our time is up"); 
      timer.endTimer(); 
      break; 
     } 
     if (!stop) 
      System.out.println("hello"); 
     else 
     { 
      if (count == 0) 
       System.out.println("we wait 10 seconds from now"); 
      count++; 
     } 


    } 
    timer.resetTimer(1200); 
    while (true) 
    { 
     if (timer.isTimeup()) 
     { 
      timer.isTimeup(); 
      System.out.println("breaking after time is up"); 
      break; 
     } 
    } 
    timer.endTimer(); 

} 

這似乎是工作,我是錯過了一些東西的威力,我需要,這是我第一次使用ScheduledExecutorService工作你們看到這個代碼有什麼問題嗎? 我不想和線程衝突發生衝突。

+0

與其讓人們瀏覽你的代碼,如果你能夠簡要介紹你的代碼在做什麼以及你在哪裏面臨問題,那就太好了。 – Arham

回答

5

隨着ScheduledExecutorService你自動獲得計時器功能。除非您擁有ScheduleExecutorService無法提供的內容,否則您不需要明確指定它。 例如假設你想在10秒的初始延遲之後開始一項任務,然後每次延遲5秒。

public void init() { 
    executor = new ScheduledThreadPoolExecutor(corePoolSize); 
    executor.scheduleWithFixedDelay(new WorkerThread(), 10, 5, TimeUnit.SECONDS); 
} 

public void end() { 
    executor.shutdown(); 
} 
+0

我需要製作一個計時器,當計時器沒有超時(在計時器超時之前完成某個任務)時,計時器將重置該計時器。我知道如何啓動一個新的調度程序,以及如何結束它。你將如何重置它?以及如何阻止它?也許這應該是我的問題。 我不認爲完全結束調度程序是我想要的。 – echew

+0

謝謝,我想再讀一遍後,答案確實有幫助。 – echew

+0

很高興知道! :) – Arham