简体   繁体   English

使用ScheduledExecutorService创建计时器

[英]Creating a timer with ScheduledExecutorService

So I'm trying to figure out how to create a timer, I came across this: using ScheduledExecutorService to start and stop timer 所以我想弄清楚如何创建一个计时器,我遇到了这个: 使用ScheduledExecutorService来启动和停止计时器

The example they have seems to work pretty well. 他们的例子看起来效果很好。 I'm just wondering if I'm utilizing this correctly: 我只是想知道我是否正确使用它:

  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");
    }
  }

 }

And here is the Test: 这是测试:

 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();

}

This seems to work, I'm might of missed something that I need, this is my first time working with the ScheduledExecutorService Do you guys see any problems with this code? 这似乎有用,我可能错过了我需要的东西,这是我第一次使用ScheduledExecutorService你们看到这个代码有什么问题吗? I don't want there to be conflict with thread collision. 我不希望与线程冲突发生冲突。

With ScheduledExecutorService you automatically get the timer feature. 使用ScheduledExecutorService,您可以自动获取计时器功能。 You don't need it explicitly unless you have something that the ScheduleExecutorService can't provide. 除非您有ScheduleExecutorService无法提供的内容,否则您不需要它。 eg Let say you want to start a task after initial delay of 10 secs and then subsequent delays of 5 seconds each. 例如,假设你想在10秒的初始延迟后开始任务,然后每次延迟5秒。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM