curiosity about java.util.Timer

Discussion in 'Java' started by Daisy, Sep 12, 2005.

  1. Daisy

    Daisy Guest

    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
     
    Daisy, Sep 12, 2005
    #1
    1. Advertising

  2. Daisy wrote:
    > 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();

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Sep 12, 2005
    #2
    1. Advertising

  3. >
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Sep 12, 2005
    #3
  4. Thomas Fritsch wrote:
    > Daisy wrote:
    >
    >> 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
     
    Steve Horsley, Sep 12, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Glick

    Naming java.util.Timer thread

    Brian Glick, Jul 30, 2003, in forum: Java
    Replies:
    1
    Views:
    645
    Gordon Beaton
    Jul 30, 2003
  2. FET
    Replies:
    3
    Views:
    528
    Chris Smith
    Sep 24, 2004
  3. amy j
    Replies:
    4
    Views:
    922
    Alex Hunsley
    Oct 13, 2004
  4. Replies:
    3
    Views:
    461
    Wesley Hall
    Dec 1, 2006
  5. Replies:
    8
    Views:
    635
    Jorgen Grahn
    Jul 15, 2013
Loading...

Share This Page