Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Thread loop cycle time

Thread view: 
wanwan - 26 Apr 2006 13:48 GMT
I'm trying to measure the average cycle time for a loop in a thread
similar to this one:

public void run() {
   while (true){
     [do something]
     Thread.sleep(1000);
   }
}

I took the measurements at the beginning of the loop 200 times and
found an average of 996ms. How can it be possible!

I need to find out the time for each cycle so to find out how much time
I need the thread to sleep.

Can anyone comment on my odd measurements?
Gordon Beaton - 26 Apr 2006 14:28 GMT
> I took the measurements at the beginning of the loop 200 times and
> found an average of 996ms. How can it be possible!
>
> I need to find out the time for each cycle so to find out how much
> time I need the thread to sleep.

If you want your loop times to be accurate, you need to implement a
PLL:

 boolean done = false;
 long now = System.currentTimeMillis();
 long next = now;
 long interval = 10; // delay, in milliseconds

 while (!done) {
   // do your stuff here...

   // sleep until next event is due
   next += interval;
   while (next > ((now = System.currentTimeMillis()))) {
     try {
       Thread.sleep(next - now);
     }
     catch (InterruptedException e) {}
   }
 }

Just make sure "your stuff" needs less time than the interval you
specify.

/gordon

Signature

[  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

wanwan - 26 Apr 2006 16:19 GMT
but I don't think System.currentTimeMillis is accurate though.  Same
number shows for up to 4 consecutive loops
ronybaryo@gmail.com - 26 Apr 2006 16:28 GMT
Hi,

This is because the cpu is very fast and not even one msec passed since
last call.

And about your first topic the sleep is not per msec reliable it can be
~10 msec more or less so its possible to sleep(1000) and get 996
average.

Roni
Gordon Beaton - 26 Apr 2006 17:18 GMT
> but I don't think System.currentTimeMillis is accurate though.  Same
> number shows for up to 4 consecutive loops

Realize that System.currentTimeMillis() is not accurate to 1 ms on all
systems. Even on systems where it is, you will get the same value
multiple times if you call it again within 1 ms. What else would you
expect?

In the code I posted, individual passes through the loop will jitter
if System.currentTimeMillis() not as accurate as you want, and this is
unavoidable. However the long term error is zero because the short
term errors on each pass do not accumulate, and variations in the
"your stuff" part of the loop are compensated for.

/gordon

Signature

[  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

Thomas Hawtin - 26 Apr 2006 17:47 GMT
>> but I don't think System.currentTimeMillis is accurate though.  Same
>> number shows for up to 4 consecutive loops
[quoted text clipped - 3 lines]
> multiple times if you call it again within 1 ms. What else would you
> expect?

System.nanoTime should be better (from 1.5). It theoretically isn't
affected by changing the system time either.

> In the code I posted, individual passes through the loop will jitter
> if System.currentTimeMillis() not as accurate as you want, and this is
> unavoidable. However the long term error is zero because the short
> term errors on each pass do not accumulate, and variations in the
> "your stuff" part of the loop are compensated for.

On average when you call System.currentTimeMillis, Thread.sleep or
anything else, you will be halfway between increments. So if you wait
until it next increments, that will on average be half the period of an
increment. Therefore, you shouldn't expect sleeps to average a round time.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 26 Apr 2006 19:06 GMT
>I'm trying to measure the average cycle time for a loop in a thread
>similar to this one:

You have two problems:
1. System.currentTimeMillis in only accurate to 50 ms on some systems.
See http://mindprod.com/jgloss/time.html

2. sleep(x) just means sleep AT LEAST x ms.  There is no guarantee the
CPU will be free and the thread will start executing right away on
wakeup time.  (There is a variant of sleep that takes a finer
resolution).

The best way to handle this sort of problem is to let a Ttimer do it.
There are two flavours. See http://mindprod.com/jgloss/timer.html
They will not be perfect, but that is the best you can do without a
RTOS.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.