This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Timer IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timer IllegalStateException" Watch "Timer IllegalStateException" New topic
Author

Timer IllegalStateException

Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
Look at this code


I'm receiving IllegalStateException, timer is already canceled when I call stopTimer().
But this is a try {} catch {} ... why isn't it working ?
I even tried adding a boolean to check if timer is canceled or no, and I still get same result. It's always crashing..
Anyone knows any solution for this? And why the try statement not working ?
Thanks For your help.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

The cancel() method doesn't throw an IllegalStateException. It is the scheduleXXX() methods that throws them. Are you sure you are looking in the right place?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
I didn't know that. Thanks a lot.
This changes the problem completely. What I am trying to do is enable and disable the timer at different places. Here is an example:
The program runs timer.schedule(); will run
Some info is received timer.cancel(); will run
Some info is received timer.schedule(); will run to start the timer again.
etc...

After I stop the timer the first time, I can't start it again ? What should I do? I need to start it and cancel it as much times as requested..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Don't cancel the Timer but cancel the TimerTask instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
how to do this ?


I should cancel RemindSignInTask only ? how ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

How about calling its cancel() method?

You will need to keep a reference to the task of course.
[ September 25, 2008: Message edited by: Rob Prime ]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

I am not convinced if either technique is best -- especially if you have to constantly turn the timer on and off.

If you cancel the timer, then you need to create a new timer, and reload all of the tasks, to turn it back on.

If you cancel each task, you won't have to create a new timer, but you still need to reload all of the tasks, to turn it back on.

How about making it cooperative? Have each of the tasks check a flag. If the flag states that it is off, then the task will skip the cycle. This way, turning the feature on or off, is as simple as setting a flag.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

That has one drawback - you're stuck to the existing schedule. If you want to shift the schedule by a few seconds (a.k.a. restart it starting from now), or have a new schedule, you will have no other choice but to cancel and reschedule. If it only matters that the task occurs regularly with a fixed interval, then skipping is a good solution.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3946
    
  17

Originally posted by Rob Prime:
That has one drawback - you're stuck to the existing schedule. If you want to shift the schedule by a few seconds (a.k.a. restart it starting from now), or have a new schedule, you will have no other choice but to cancel and reschedule. If it only matters that the task occurs regularly with a fixed interval, then skipping is a good solution.



I guess we are drifting a bit from the OP's question, but would having each task be scheduled as a single-execution task that re-schedules itself at the end of the run method solve this? Instead of having the Timer control the delay, you would have a stored or calculated delay inside the TimerTask. At the end of the run you timer.schedule(this,delay) for the next occurence.

Which gives you the flexibility to use a flag to skip a cycle, an instance variable to modify schedules, and another flag to prevent rescheduling.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer IllegalStateException
 
Similar Threads
Set Timer
Help on stopping method...
Need to restart timer with different time
Threads doubt
Help needed with java.util.Timer