System.currentTimeMillis gives wrong time

A

Alex

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
 
J

Joan

Alex said:
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.
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
A

Alex

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.
 
A

Andrew Thompson

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'
 
J

john

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 ? :)
 
G

Gordon Beaton

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
 
C

Chris Uppal

Alex said:
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
 
R

Robert Klemme

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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
G

Gordon Beaton

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
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
 
J

Joan

Roedy Green said:
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
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top