aspose file tools*
The moose likes Beginning Java and the fly likes Exception in timer : IllegalStateException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in timer : IllegalStateException" Watch "Exception in timer : IllegalStateException" New topic
Author

Exception in timer : IllegalStateException

kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
In our application, we have a common timer to poll all agents.
at each time interval application try to poll on all agents using this timer.
at application level we never stop the timer, but application may restart the timer(depends on some condition) with new time period and cancel the old timer task, assign a new timer task to the timer.
The problem is while restarting polling for one agent it got the following exception,

java.lang.IllegalStateException: Timer already cancelled.

in the catch block the application try to start the polling again, which again result in same exception and result in out of memory exception for this Thread.

Can any one helps us to resolve this issuse


Kannan.DV
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

I can't really tell from your post, but I am assuming that you are referring to the java.util.Timer class. Anyway, once you cancel the timer, you can't schedule anything on it anymore -- If you need to schedule more stuff, you will need another timer.

BTW, I believe that this it also true for the time task. If you cancel the timer task, you will need another timer task.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
yes i am using java.util.timer
the problem is there is no implementation to cancel the timer in the application level - even though, we got this kind of exception.
java.lang.IllegalStateException: Timer already cancelled.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're certainly calling cancel() somewhere on the timer.

Did you know you can also cancel individual tasks? Perhaps that is a better option - keep the timer active (it won't do anything if there are no active tasks), and cancel the current task. You need to add a new task anyway if you want to restart it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
Hi Rob,
You're certainly calling cancel() somewhere on the timer.
kannan>> no, in code level there is no call to the method cancel() on this timer.

Did you know you can also cancel individual tasks?
Kannan>yes

"Perhaps that is a better option - keep the timer active (it won't do anything if there are no active tasks), and cancel the current task."

Kannan>> this is the current implementation in our application.

You need to add a new task anyway if you want to restart it.
Kannan >> but still we got the exception --
java.lang.IllegalStateException: Timer already cancelled.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

I checked the source of both Timer and TimerTask, and one of the following must have happened:
- the timer was cancelled
- the timer's internal thread was interrupted
- the timer was garbage collected (but that can't have happened because you still have a reference)

If you're 100% sure that the timer was not cancelled, check the timer task. Perhaps it is interrupting the current thread.
kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
hi rob,
i am 100% sure at code level there is no call to cancel the timer.
can you please guide us , how to find the
- the timer's internal thread was interrupted
- check the timer task. Perhaps it is interrupting the current thread.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Are you calling interrupt() on a thread anywhere? Are you anywhere catching InterruptedException and then ignoring that?
kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
Dear Rob,

Could you please repond to the following queries.

Actually, the problem in my application is similar to this posted few years back.
http://lists.agentpp.org/pipermail/snmp4j/2006-June/001399.html

I searched through google and found no solution for this.
It seems there is an OPEN BUG related to Java Timer.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4290274

I am unable to identify the root cause of the execption in this case ?
Could you shed some light on the 3 points you said above !


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

This exception is thrown on line 354 of java.util.Timer (Java 6 source; check src.zip in the JDK folder), which says:
I did a full check on when this variable is set to false, and came up with the following:
This is the only access to threadReaper, so it can only be finalized if the Timer itself is garbage collected. Therefore, this won't be the cause.
Well, you're calling cancel() nowhere, and this method is called nowhere from the API, so again, this is not the case.
That's the last occurrence, so this must be the culprit. Apparently, mainloop ends - either by returning or by throwing a RuntimeException or an Error. Non RuntimeExceptions cannot occur since these must be caught or thrown from the run method, and neither occurs.

So, let's investigate mainloop:
Ok, an infinite loop. So let's look for break, return and throw statements:
That looks like a potential cause. However, the wile loop only ends if the queue is not empty, or if newTasksMayBeScheduled is set to false. Since afterwards the queue is empty, that means that newTasksMayBeScheduled is false. But it can't have been set to false yet, because that can only have been done by calling cancel() or by the above finalizer - mainloop is still running. Both haven't occurred yet. So let's continue:
Well, that does not leave the loop. So we go forward. However, there are no more break, return or throw statements.

That leaves just two possibilities:
1) an InterruptedException is thrown, it is caught in mainloop, and silently ignored.
2) an Error or RuntimeException has been thrown.

Since you don't see anything on the console (correct?), that must mean 1) occurred. That can only occur during the two calls to queue.wait(...), and only if the thread is interrupted.


So that's my guess - the thread is somehow interrupted.
Jerome Laforge
Greenhorn

Joined: Jun 22, 2012
Posts: 1
Hello Rob,
First of all, thanks for your explanation. I don't understand your conclusion.
You told us :
That leaves just possibilities:
1) an InterruptedException is thrown, it is caught in mainloop, and silently ignored.
2) an Error or RuntimeException has been thrown.

I am surely miss something for the first case (InterruptedException is thrown), but how InterruptedException can leave the infinite loop?
Because as you told us, there is "catch(InterruptedException e)" inside this infinite loop that has to catch this exception. For me, this exception is catched here into this loop and since it is not thrown again, the timer thread doesn't leave this loop.

Am I wrong?

Thanks in advance for your reply
Best regard
Jerome
 
wood burning stoves
 
subject: Exception in timer : IllegalStateException