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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With