• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in timer : IllegalStateException

 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Henry Wong
author
Marshal
Pie
Posts: 21022
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you calling interrupt() on a thread anywhere? Are you anywhere catching InterruptedException and then ignoring that?
 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic