Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether the Timer task has completed

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?

like image 632
Pavan Avatar asked Dec 05 '12 11:12

Pavan


People also ask

How do you end a Timer task?

The cancel() method is used to cancel the timer task. The cancel() methods returns true when the task is scheduled for one-time execution and has not executed until now and returns false when the task was scheduled for one-time execution and has been executed already.

What is a Timer task?

TimerTask is an abstract class defined in java. util package. TimerTask class defines a task that can be scheduled to run for just once or for repeated number of time. In order to define a TimerTask object, this class needs to be implemented and the run method need to be overridden.

What is Timer schedule?

The schedule (TimerTask task, Date firstTime, long period) is the method of Timer class. It is used to schedule the given task again and again in given fixed time execution.


1 Answers

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
like image 84
DroidBender Avatar answered Oct 04 '22 08:10

DroidBender