wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes How to avoid IllegalStateExceotion in Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to avoid IllegalStateExceotion in Timer" Watch "How to avoid IllegalStateExceotion in Timer" New topic
Author

How to avoid IllegalStateExceotion in Timer

Deepak Kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 15
Hi,

I am using java Timer and TimerTask for the scheduling operation. When my schedule task breaks due to some exception the timer it scheduled on will be canceled automatically. This is creating problem when i try to schedule new task om the same timer, it throws "IllegalStateException- Timer already canceled"

It will be great help if any one could tell me how to overcome this problem.

Thanking in advance
Deepak Vernekar
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you want the Timer to continue even if one of the tasks has an exception, you need to catch any exception before it exits the TimerTask's run() method. Here is one way to achieve that:


"I'm not back." - Bill Harding, Twister
Deepak Kumar
Greenhorn

Joined: Jul 21, 2005
Posts: 15
Thanks Jim,

i am doing exactly the way you mentioned above, but still it is not working. I read in below the article that once the timerTask exits with the error the java timer will set the boolean "newTasksMayBeScheduled = false;". Then onwards this will not allow any other task to be scheduled.

I think i am clear what i want to say??
FYI: http://cougaar.org/pipermail/cougaar-developers/2005-June/001867.html

Thanks,
Deepak Vernekar
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Deepak]: i am doing exactly the way you mentioned above, but still it is not working. I read in below the article that once the timerTask exits with the error the java timer will set the boolean "newTasksMayBeScheduled = false;". Then onwards this will not allow any other task to be scheduled.

That is correct. You can also see this by looking at the source code tor Timer, or more specifically at teh java.util.TimerThread class defined in the same file as Timer. Look at the run() and mainLoop() methods. Anyway: once a TimerTask exits the run() method with an exception, there is no way to make the Timer work again. Therefore, you must prevent the TimerTask from exiting with an exception. That is the purpose of the SafeTimerTask, to trap the exception. You must use this in place of all other TimerTasks , and you must use it beforethe exception occurs. Once the exception occurs, it's too late.

I suppose another alternative is to replace the Timer with a new Timer. However the new timer won't know anything about all the other TimerTasks that were scheduled in the old Timer. So I think using SafeTimerTask is preferred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to avoid IllegalStateExceotion in Timer