Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (426)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 1383 times)
0 Members and 1 Guest are viewing this topic.
Online EgonOlsen
« Posted 2004-10-15 16:18:27 »

Hi,

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: 1


pixels! :x


« Reply #1 - Posted 2004-10-15 16: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 Member




Agency9


« Reply #2 - Posted 2004-10-15 21: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
Online EgonOlsen
« Reply #3 - Posted 2004-10-15 22: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 Member





« Reply #4 - Posted 2004-10-17 10: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!

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4500388
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4717583

You could also try running with this JVM flag:
-XX:+ForceTimeHighResolution
Online EgonOlsen
« Reply #5 - Posted 2004-10-17 13: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  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (75 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org 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‑gaming.org
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!