Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Too much sleep(xx)  (Read 2701 times)
0 Members and 1 Guest are viewing this topic.
Offline EgonOlsen
« Posted 2004-10-15 14:18:27 »


i recently discovered a strange behaviour regarding sleep on my machines that i wanted to share. Maybe somebody has an idea what causes this. Here we go:

Consider an endless loop doing nothing but a Thread.sleep(20). How many times can it do this in 1 sec?

On my old Celeron 1Ghz/XP/Java 1.4.2, the answer is 50 (as expected)
On my P4HT 3.2Ghz/XP/Java 1.4.2/Java 1.5, the answer is 33 (for both VMs)
On my PII-400/Debian Linux/Java 1.4.2, the answer is 33.

So far, so good. I changed the value to 19 instead and got 52 on the Celeron, 36 on Linux...but i got 52 on the P4 too. After changing it to 21, i got 48 on the Celeron, 48 on the P4 and 28 on Linux.

I would be able to understand that, if the Celeron would always be correct and Linux and the P4 won't for whatever reason...but why is the P4 like the Celeron for 19 and 21 and like the Linux machine for 20 Huh

Any ideas?

Offline oNyx

JGO Coder

Medals: 2

pixels! :x

« Reply #1 - Posted 2004-10-15 14:44:40 »

Well, by calling sleep you give away the control (the OS' scheduler takes care then) and if you are lucky you get the cpu back in time... but there is no guarantee.

The shortest time you can sleep (on all OSs) is about 5msec. For that reason most games don't sleep at all... they yield() instead.

弾幕 ☆ @mahonnaiseblog
Offline Tomas

Junior Devvie


« Reply #2 - Posted 2004-10-15 19:33:43 »

Since System.currentTimeMillies() only gives you an accuracy of 10ms. Then it is likley that Thread.sleep() behaves as poorly.

CTO Agency9
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline EgonOlsen
« Reply #3 - Posted 2004-10-15 20:55:39 »

It may behave poorly and be inaccurate...i understand all that. But i really don't understand why 19 and 21 works on this machine as expected and 20 acts as if i would have choosen 30...that makes no sense to me.

Offline Mark Thornton

Senior Devvie

« Reply #4 - Posted 2004-10-17 08:45:46 »

Firstly your P4 is probably hyperthreaded. This means that the normal clock period is 15.625ms instead of the 10ms usually found on single processor machines (under Windows anyway). This explains the approximately 30ms intervals you are seeing. The Celeron is not HT so it will use the normal 10ms period which fits nicely with a 20ms request.

So what happens with the 'odd' requests. To support multimedia use Windows allows the clock period to be changed. Recent JVM do this when the requested interval is short and not a multiple of what they think is the clock period (i.e. they appear to assume the clock period is 10ms even on HT/dual processor machines). I haven't seen any documentation on the exact logic used. The effect is that a request for either 19 or 21ms will cause the clock period to be reduced to perhaps 1ms, which then enables the OS thread scheduler to sleep for close to the requested period.

Even more exciting is that the clock period is a global property for the machine. Thus if a separate process is granted a short clock period, then your process may suddenly find sleep intervals becoming more (or even less) accurate!

You could also try running with this JVM flag:
Offline EgonOlsen
« Reply #5 - Posted 2004-10-17 11:18:54 »

Thank you for your answer. It really explains the behaviour i'm seeing. I already thought that it has something to do with HT, but i wasn't aware that the clock period is actually different. I'll try that flag later and see if it helps.

Pages: [1]
  ignore  |  Print  

hadezbladez (335 views)
2018-11-16 13:46:03

hadezbladez (177 views)
2018-11-16 13:41:33

hadezbladez (336 views)
2018-11-16 13:35:35

hadezbladez (82 views)
2018-11-16 13:32:03

EgonOlsen (2175 views)
2018-06-10 19:43:48

EgonOlsen (2203 views)
2018-06-10 19:43:44

EgonOlsen (1376 views)
2018-06-10 19:43:20

DesertCoockie (2008 views)
2018-05-13 18:23:11

nelsongames (1646 views)
2018-04-24 18:15:36

nelsongames (2297 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!