system clock running too fast

R

Raj

Hi,

I have a java application (1.3.1_01) using multiple threads.

When a new code(to scroll text) is added using thread with sleep
interval 17, the clock is running too fast i.e., in 10 minutes of
regular time it is 25 minutes.

When the thread sleep interval is increased to 60, the clock is running
at normal rate but the scrolling text has slowed down.

Any alternative solutions that can help without impacting scrolling
text speed?

Thanks
 
O

Oliver Wong

Raj said:
Hi,

I have a java application (1.3.1_01) using multiple threads.

When a new code(to scroll text) is added using thread with sleep
interval 17, the clock is running too fast i.e., in 10 minutes of
regular time it is 25 minutes.

When the thread sleep interval is increased to 60, the clock is running
at normal rate but the scrolling text has slowed down.

Any alternative solutions that can help without impacting scrolling
text speed?

I suspect that the clock is actually NOT running too fast, but rather
you are using a variable to store how much time you think has passed, and
you are incrementing that variable too much.

How about posting a SSCCE? http://www.physci.org/codes/sscce.jsp

- Oliver
 
R

Roedy Green

When a new code(to scroll text) is added using thread with sleep
interval 17, the clock is running too fast i.e., in 10 minutes of
regular time it is 25 minutes.

A display clock should check the system time, not simply count sleep
intervals. A sleep in Java is not guaranteed to wake precisely on
time. It merely get scheduled to awaken when there is free CPU time.

Without seeing your code we can only guess wildly why it does not
work. It a bit like going to the doctor and saying "My mother is
feeling poorly. What is the matter with her?"
 
C

Chris Smith

Raj said:
I have a java application (1.3.1_01) using multiple threads.

When a new code(to scroll text) is added using thread with sleep
interval 17, the clock is running too fast i.e., in 10 minutes of
regular time it is 25 minutes.

When the thread sleep interval is increased to 60, the clock is running
at normal rate but the scrolling text has slowed down.

When you're doing something in a timer (for example, incrementing a
clock from a thread), there are two ways to do it. One shoots for a
consistent delay, and the other for a consistent rate. A fixed-rate
loop will correct for inaccuracies. For example, if the first call to
sleep doesn't wait quite long enough, then the next one will wait longer
to compensate... and vice versa. You're probably writing the fixed-
delay form of loop instead. To increment a clock, you want a fixed
rate.

The fixed delay code might look something like this:

while (!done)
{
doSomething();
Thread.sleep(1000);
}

Fixed rate code will look like this instead.

final long next = System.currentTimeMillis();

while (!done)
{
long delay = next - System.currentTimeMillis();
if (delay > 0) Thread.sleep(delay);

doSomething();
next += 1000;
}


Now that you understand the theory, let me point you toward
java.util.Timer, which does all the hard work for you.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top