System.currentTimeMillis gives wrong time

Discussion in 'Java' started by Alex, Sep 6, 2005.

  1. Alex

    Alex Guest

    In my program I have a System.currentTimeMillis() in a loop. Every
    couple second I print out current time returned by this function on the
    screen.
    I returns correct time but after running for about an hour it starts
    returning time which is 10 minutes late. It happens not once but in
    multiple cycles.
    Then eventuall it starts returning correct time again.

    It really puzzles me. Do you have any suggestions.
    This is a multithreaded application.

    Thanks,
    Alex
     
    Alex, Sep 6, 2005
    #1
    1. Advertising

  2. Alex

    Joan Guest

    "Alex" <> wrote in message
    news:...
    > In my program I have a System.currentTimeMillis() in a loop.
    > Every
    > couple second I print out current time returned by this
    > function on the
    > screen.
    > I returns correct time but after running for about an hour it
    > starts
    > returning time which is 10 minutes late. It happens not once
    > but in
    > multiple cycles.
    > Then eventuall it starts returning correct time again.
    >
    > It really puzzles me. Do you have any suggestions.
    > This is a multithreaded application.
    >
    > Thanks,
    > Alex


    I did a similar thing with a JLabel, but it only got behind a
    second or two.
    I think the thread you are using for this is not running as often
    as it
    needs to.
     
    Joan, Sep 7, 2005
    #2
    1. Advertising

  3. Alex

    Roedy Green Guest

    On 6 Sep 2005 15:58:48 -0700, "Alex" <> wrote or
    quoted :

    >It really puzzles me. Do you have any suggestions.


    Is the output getting stuck in a buffer for 10 minutes?

    Is your clock in the bottom right of your screen accurate?

    Are you running something high priority and so CPU intensive that
    could be causing timer ticks to be lost?

    You can try running SetClock at various times during your run to see
    how badly out it is really, verse what you buffer says.

    see http://mindprod.com/jgloss/setclock.html

    Clock malfunctions have not been much of a problem since DOS days, so
    I most strongly suspect the problem is buffering. Try flush.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 7, 2005
    #3
  4. Alex

    Roedy Green Guest

    On 6 Sep 2005 15:58:48 -0700, "Alex" <> wrote or
    quoted :

    >In my program I have a System.currentTimeMillis() in a loop. Every
    >couple second I print out current time returned by this function on the
    >screen.


    "print" has many different meanings. If you mean display on the
    screen, perhaps the thread that is updating the display is getting
    blocked or not triggered often enough.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 7, 2005
    #4
  5. Alex

    Alex Guest

    Simplified example of what's happening:
    System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    etc. and then suddenly 1000 again. It's the same thread. It's not just
    a delay because of the buffer problems. It literally goes back in time
    :)
    I use System.out.prinln() to print time.

    I can't even imagine what might cause this.
     
    Alex, Sep 7, 2005
    #5
  6. On 6 Sep 2005 23:44:17 -0700, Alex wrote:

    > Simplified example of what's happening:
    > System.currentTimeMillis() gives me lets say


    *

    >..1000, 1001, 1002, 1003
    > etc. and then suddenly 1000 again.


    * 'Lets say' you give us numbers from an actual run, rather
    than numbers that you have positted based on your (lack of)
    understanding of the problem.

    As an aside, OS's generally have a 'time granularity' that
    is around 20-50mseconds, from memory.

    What is the biggest drift from '1000' that you have seen?

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "The storm broke with a violent flash of lightning, and an apalling crash
    of thunder.."
    Severed Heads 'Dead Eyes Opened'
     
    Andrew Thompson, Sep 7, 2005
    #6
  7. Alex

    Guest

    > Simplified example of what's happening:
    > System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    > etc. and then suddenly 1000 again. It's the same thread. It's not just
    > a delay because of the buffer problems. It literally goes back in time. :)
    > I use System.out.prinln() to print time. I can't even imagine what
    > might cause this.


    I gues you are stuck in an episode of The Twilight Zone ...

    Do you see other strange things happening around you ?
    Is there a talking cat with you, or multiple clones of your
    spouse ? :)
     
    , Sep 7, 2005
    #7
  8. On 6 Sep 2005 23:44:17 -0700, Alex wrote:
    > Simplified example of what's happening:
    > System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    > etc. and then suddenly 1000 again. It's the same thread. It's not just
    > a delay because of the buffer problems. It literally goes back in time
    >:)
    > I use System.out.prinln() to print time.
    >
    > I can't even imagine what might cause this.


    Another process changing the system time.

    On Unix, ntp does this transparently without causing the time to jump
    or go backwards.

    On Windows, I have no idea but it wouldn't surprise me if
    "automatically synchronize time with an internet server" made that
    kind of periodic adjustment.

    On the other hand, there could very well be something wrong with the
    code you neglected to post.

    In any case, I seriously doubt that System.currentTimeMillis() itself
    is the problem.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 7, 2005
    #8
  9. Alex

    Chris Uppal Guest

    Alex wrote:

    > Simplified example of what's happening:
    > System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    > etc. and then suddenly 1000 again. It's the same thread. It's not just
    > a delay because of the buffer problems. It literally goes back in time
    > :)
    > I use System.out.prinln() to print time.
    >
    > I can't even imagine what might cause this.


    Neither can I. Which makes me suspect that (however unlikely it may seem)
    there must be a problem in your code. Can you reduce it to a self contained
    example that is small enough to post ?

    -- chris
     
    Chris Uppal, Sep 7, 2005
    #9
  10. On Wed, 7 Sep 2005 09:19:19 +0100, Chris Uppal wrote:

    > Can you reduce it to a self contained
    > example that is small enough to post ?


    For tips on preparing self contained (small) examples, see..
    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "If we fall, others are rising.."
    Paul Kelly 'From Little Things, Big Things Grow'
     
    Andrew Thompson, Sep 7, 2005
    #10
  11. Chris Uppal wrote:
    > Alex wrote:
    >
    >> Simplified example of what's happening:
    >> System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    >> etc. and then suddenly 1000 again. It's the same thread. It's not
    >> just a delay because of the buffer problems. It literally goes back
    >> in time :)
    >> I use System.out.prinln() to print time.
    >>
    >> I can't even imagine what might cause this.


    Someone or something actually changed the clock. Or it's due to the OS
    specific inaccuracy of time values (I believe on Windows it's 10ms). Or
    the time values are printed in non chronological order (unlikely if it's
    just a single thread). Or you accidentally reuse an old value in some
    places of your code. Just some ideas...

    Kind regards

    robert
     
    Robert Klemme, Sep 7, 2005
    #11
  12. Alex

    Roedy Green Guest

    On 6 Sep 2005 23:44:17 -0700, "Alex" <> wrote or
    quoted :

    >System.currentTimeMillis() gives me lets say 1000, 1001, 1002, 1003
    >etc. and then suddenly 1000 again.


    Do you have some software package that automatically corrects the time
    periodically? Perhaps your clock is so out of whack every time it
    kicks it is sends you back in time.

    Does this happen on any other machine? If not consider taking your
    machine to someone who has motherboard diagnostic software to check
    out the two clocks, the timer tick and the time of day.

    Here is one other experiment you might do over a weekend. Just leave
    your machine running but don't do anything. Run SetClock at the
    beginning and end of the weekend to see how badly you clock is out.
    See http://mindprod.com/webstarts/setclock.html It should not be out
    my more than a second or two.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 8, 2005
    #12
  13. Alex

    Roedy Green Guest

    On 7 Sep 2005 10:00:19 +0200, Gordon Beaton <> wrote or
    quoted :

    >On Windows, I have no idea but it wouldn't surprise me if
    >"automatically synchronize time with an internet server" made that
    >kind of periodic adjustment.


    That sort of tools is SUPPOSED to make the adjustment smoothly. In
    Java, the time of day clock jumping can cause task sleep times to be
    totally wrong.

    You really should not adjust the time of day clock when a Java program
    is running, unless it is expecting it.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 8, 2005
    #13
  14. Alex

    Roedy Green Guest

    On Wed, 7 Sep 2005 13:39:31 +0200, "Robert Klemme" <>
    wrote or quoted :

    >Someone or something actually changed the clock. Or it's due to the OS
    >specific inaccuracy of time values (I believe on Windows it's 10ms). Or
    >the time values are printed in non chronological order (unlikely if it's
    >just a single thread). Or you accidentally reuse an old value in some
    >places of your code. Just some ideas...


    You may be reusing an old variable because of a tiny spelling error.
    Without code to look at it is just a shot in the dark.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 8, 2005
    #14
  15. On Thu, 08 Sep 2005 08:25:24 GMT, Roedy Green wrote:
    > On 7 Sep 2005 10:00:19 +0200, Gordon Beaton <> wrote or
    > quoted :
    >
    >>On Windows, I have no idea but it wouldn't surprise me if
    >>"automatically synchronize time with an internet server" made that
    >>kind of periodic adjustment.

    >
    > That sort of tools is SUPPOSED to make the adjustment smoothly.


    Certainly that would be the preferred way to do it, but the fact that
    the little window where you enable that function has a text along the
    lines of "next adjustment: <approx 15 minutes from now>" makes me
    wonder if it really is the case.

    > In Java, the time of day clock jumping can cause task sleep times to
    > be totally wrong.
    >
    > You really should not adjust the time of day clock when a Java
    > program is running, unless it is expecting it.


    Programs written in Java are no different from any other programs in
    this respect.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 8, 2005
    #15
  16. Alex

    Joan Guest

    "Roedy Green" <> wrote in message
    news:eek:...
    > On 6 Sep 2005 23:44:17 -0700, "Alex" <> wrote
    > or
    > quoted :
    >
    >>System.currentTimeMillis() gives me lets say 1000, 1001, 1002,
    >>1003
    >>etc. and then suddenly 1000 again.

    >
    > Do you have some software package that automatically corrects
    > the time
    > periodically? Perhaps your clock is so out of whack every time
    > it
    > kicks it is sends you back in time.
    >
    > Does this happen on any other machine? If not consider taking
    > your
    > machine to someone who has motherboard diagnostic software to
    > check
    > out the two clocks, the timer tick and the time of day.


    The NTP package is very sophisticated, but it is the user's
    machine that
    decides how to adjust its own clock. Sun, for example, has had
    NTP software
    as part of the OS for at least 5 years now. It defaults to a mode
    where the
    OS updates the system time very slowly.

    > Here is one other experiment you might do over a weekend. Just
    > leave
    > your machine running but don't do anything. Run SetClock at the
    > beginning and end of the weekend to see how badly you clock is
    > out.
    > See http://mindprod.com/webstarts/setclock.html It should not
    > be out
    > my more than a second or two.


    For windows, you can manually set the clock off by a few seconds
    then use
    "update now" on the "internet time" tab and see what happens;
    fast or slow.
    The default is to sync once every 7 days.

    BTW, have you noticed that MS has changed the default time server
    from
    time.nist.gov to time.windows.com
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > http://mindprod.com Again taking new Java programming
    > contracts.
     
    Joan, Sep 8, 2005
    #16
    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. Nelson
    Replies:
    17
    Views:
    9,203
    Darryl L. Pierce
    May 20, 2004
  2. Jerry

    System.currentTimeMillis()

    Jerry, Aug 3, 2005, in forum: Java
    Replies:
    18
    Views:
    46,506
    Thomas G. Marshall
    Aug 6, 2005
  3. Roedy Green
    Replies:
    8
    Views:
    1,208
    Daniel Dyer
    Mar 9, 2006
  4. neoedmund
    Replies:
    1
    Views:
    1,190
  5. Replies:
    17
    Views:
    4,182
Loading...

Share This Page