While working with Timer and TimerTask,Sometime we will get the below Exception
Exception in thread "main" java.lang.IllegalStateException: Task already scheduled or cancelled
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.schedule(Unknown Source)
The exception may be due to any one of the below scenario
(1)We would have scheduled the Same Timetask object(same reference) ,which was scheduled alreadyWe (2)would have scheduled TimerTask on the cancelled Timer
Lets see the below code example to understand this.
(1) Exception due to Scheduling the same Timer Task object reference
Lets imagine our requirement is to schedule a task to run after 15 and 25 seconds.Hence we schedule the task twice as shown below
When we run the above program it will throw the exception "java.lang.IllegalStateException: Task already scheduled or cancelled",since we have used the same timer task object reference for scheduling twice.To resolve this,always create new Timer Task object for scheduling the Timer everytime as shown in the below code.
(2) Exception due to Scheduling the Timer Task on cancelled Timer
For some requirement,we may like to cancel all the Scheduled Task when some condition meet.
Calling the cancel method on the Timer object,will clear all the scheduled task on the Timer object.
Thus all the scheduled task will not run and if we try to schedule any more task on the cancelled timer will throw the exception "java.lang.IllegalStateException: Timer alreadycancelled" .The below code block will show this.
To resolve this,we need to re instantiate the timer object and then the task has to be scheduled like below code.
Related Topics:
Example - Timer and Timertask in Java
TimerTask - Choosing the correct Schedule Method
Exception in thread "main" java.lang.IllegalStateException: Task already scheduled or cancelled
at java.util.Timer.sched(Unknown Source)
at java.util.Timer.schedule(Unknown Source)
The exception may be due to any one of the below scenario
(1)We would have scheduled the Same Timetask object(same reference) ,which was scheduled alreadyWe (2)would have scheduled TimerTask on the cancelled Timer
Lets see the below code example to understand this.
(1) Exception due to Scheduling the same Timer Task object reference
Lets imagine our requirement is to schedule a task to run after 15 and 25 seconds.Hence we schedule the task twice as shown below
public class TimerExample { public static void main(String[] args) { TimerExample timerExample= new TimerExample(); timerExample.timerDemoForException(); /*Have an infinite while loop,so that Main progran wont be terminated*/ while (true) { } } public void timerDemoForException() { Timer timer= new Timer(); Clock clock= new Clock(); timer.schedule(clock, 15*1000);//to Schedule the task to run after 15 seconds timer.schedule(clock, 25*1000);//to Schedule the task to run after 25 seconds } class Clock extends TimerTask { @Override public void run() { System.out.println("Now the Tume is:"+new Date()); } } |
When we run the above program it will throw the exception "java.lang.IllegalStateException: Task already scheduled or cancelled",since we have used the same timer task object reference for scheduling twice.To resolve this,always create new Timer Task object for scheduling the Timer everytime as shown in the below code.
public void TimerDemoForException() { Timer timer= new Timer(); Clock clock= new Clock(); timer.schedule(clock, 5*1000); clock= new Clock();//Instantiate the new Task object timer.schedule(clock, 15*1000); } |
(2) Exception due to Scheduling the Timer Task on cancelled Timer
For some requirement,we may like to cancel all the Scheduled Task when some condition meet.
Calling the cancel method on the Timer object,will clear all the scheduled task on the Timer object.
Thus all the scheduled task will not run and if we try to schedule any more task on the cancelled timer will throw the exception "java.lang.IllegalStateException: Timer alreadycancelled" .The below code block will show this.
public void timerDemoForException() { Timer timer= new Timer(); Clock clock= new Clock(); timer.schedule(clock, 10*1000); timer.cancel(); clock= new Clock(); timer.schedule(clock, 15*1000); } |
To resolve this,we need to re instantiate the timer object and then the task has to be scheduled like below code.
public void timerDemoForException() { Timer timer= new Timer(); Clock clock= new Clock(); timer.schedule(clock, 10*1000); timer.cancel(); timer= new Timer();//Instantiate the new Timer object clock= new Clock(); timer.schedule(clock, 15*1000); } |
Related Topics:
Example - Timer and Timertask in Java
TimerTask - Choosing the correct Schedule Method
Thanks for much this post.
ReplyDeleteYou save me