curiosity about java.util.Timer

D

Daisy

I had a problem that I fixed, but aroused my curiosity. I'd appreciate
any insights you have.

I had a Timer (java.util not Swing) cancel in my server over the three
day weekend a week ago. I'll fix the problem by instantiating a new
Timer instance when I catch the IllegalState error. However, I'm
curious why the timer would cancel.
1. There is no call to Timer. cancel() in the code
2. This code has worked well for more than a year

The Java API says," After the last live reference to a Timer object
goes away and all outstanding tasks have completed execution, the
timer's task execution thread terminates gracefully (and becomes
subject to garbage collection). However, this can take arbitrarily long
to occur."

I'm wondering if the 3 days of without a task constitutes arbitrarily
long?

I'm running JRockit 1.5 on SuSE 8.3

Thanks
 
T

Thomas Fritsch

Daisy said:
I had a problem that I fixed, but aroused my curiosity. I'd appreciate
any insights you have.
Curiosity is always a good thing.
I had a Timer (java.util not Swing) cancel in my server over the three
day weekend a week ago. I'll fix the problem by instantiating a new
Timer instance when I catch the IllegalState error. However, I'm
curious why the timer would cancel.
1. There is no call to Timer. cancel() in the code
2. This code has worked well for more than a year
Take a close look into the "Timer.java" source (it is in "src.zip" of
your JDK directory):
The TimerThread.run() method [in Timer.java, too] also ends the timer
when the thread got an Exception/Error. It does so with these 2 lines:
newTasksMayBeScheduled = false;
queue.clear();
This has the same effect as what the Timer.cancel() method does. There
are the same 2 lines:
thread.newTasksMayBeScheduled = false;
queue.clear();
 
T

Thomas Hawtin

I had a Timer (java.util not Swing) cancel in my server over the three
day weekend a week ago. I'll fix the problem by instantiating a new
Timer instance when I catch the IllegalState error. However, I'm
curious why the timer would cancel.
1. There is no call to Timer. cancel() in the code

I assume you the IllegalState error says "Timer already cancelled.", not
"Task already scheduled or cancelled".

Looking at the code a task could get canceled if it is resurrected from
an object finalising. That is a bit unlikely.

More likely is the task throwing an exception, or the thread getting
interrupted.

As it uses System.currentTimeMillis I guess clumsy changes to the system
clock could cause problems, but probably not this.
2. This code has worked well for more than a year

Doesn't mean it is safe. Ask a NASA manager.
The Java API says," After the last live reference to a Timer object
goes away and all outstanding tasks have completed execution, the
timer's task execution thread terminates gracefully (and becomes
subject to garbage collection). However, this can take arbitrarily long
to occur."

I'm wondering if the 3 days of without a task constitutes arbitrarily
long?

Timer uses a finaliser to stop the thread. It's mostly down to garbage
collection when that gets called. If the timer is long lived, then it
will take major collection to shift it. If the server is not creating
many medium lived objects, then there is no reason for a major collection.

Tom Hawtin
 
S

Steve Horsley

Thomas said:
Daisy said:
I had a problem that I fixed, but aroused my curiosity. I'd appreciate
any insights you have.

Curiosity is always a good thing.
I had a Timer (java.util not Swing) cancel in my server over the three
day weekend a week ago. I'll fix the problem by instantiating a new
Timer instance when I catch the IllegalState error. However, I'm
curious why the timer would cancel.
1. There is no call to Timer. cancel() in the code
2. This code has worked well for more than a year
Take a close look into the "Timer.java" source (it is in "src.zip" of
your JDK directory):
The TimerThread.run() method [in Timer.java, too] also ends the timer
when the thread got an Exception/Error. It does so with these 2 lines:
newTasksMayBeScheduled = false;
queue.clear();
This has the same effect as what the Timer.cancel() method does. There
are the same 2 lines:
thread.newTasksMayBeScheduled = false;
queue.clear();

To my mind, that's downright irresponsible. It's not documented
anywhere, and why on earth anyone would choose to write such a
fragile implementation is completely beyond me.

But the solution must be that every TimerTask must catch every
exception, not let them propogate up to kill the timer.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top