How to determine whether the Timer task has completed

Pavan picture Pavan · Dec 5, 2012 · Viewed 17.4k times · Source

I have this following code :

Timer timer = new Timer();      
TimerTask task = new TimerTask() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
    }
};

timer.schedule(task, 10000);//execute after 10 seconds

Can we determine whether the task is already executed by the timer or is still due?

Answer

DroidBender picture DroidBender · Dec 5, 2012

Add a simple variable like..

  boolean isTaskCompleted = false;
  Timer timer = new Timer();      
  TimerTask task = new TimerTask() {

     @Override
     public void run() {
        // do stuff
        isTaskCompleted = true;
     }
  };

  timer.schedule(task, 10000);//execute after 10 seconds