Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  Any way to get near 100th nanosecond precision?  (Read 6762 times)
0 Members and 1 Guest are viewing this topic.
Offline Jeffrey_Chee

Senior Newbie

Java games rock!

« Posted 2002-12-28 20:22:33 »

Hi, I posted this question in the java forums at and din't seem to get many good responses.

After searching through the forums, I realised the inaccuracy of the Thread.sleep(long millis, int nanos) method, which I used in my musical game.( Some other thread titled "try my musical game")

This could be the main reason I'm getting of tempo midi music....

Is there a workaround to it? or should I say is there a way to do it within the JDK libraries (non JNI, non Java3D, etc) or any kind of custom code that makes use of the standard libraries to be able to get nanosecond precision?

I din't really need 1 nanosec precision, but maybe in the 100 nanos.. (0.1 microsec)

thanks in advance

PS: I've read about the sleep based timer hack, and I don't think it'll work in this case where I need 0.1 micro instead of 1 milli... and I've read elsewhere that the method sleeps to the System timer's granularity of time. (i.e 50 ms for win ME, 1 ms for XP, etc etc)

Offline Pyros

Senior Newbie

Let's make Java games rock !

« Reply #1 - Posted 2002-12-28 20:27:28 »

You probably won't get it though the standard API, as Java runs on so many platforms it can't promise such accuracy anyhow. Maybe JNI or something will help get through this (anyone?). The reason Thread.sleep() won't work is that you cannot make the thread start immediately after sleep. You can only say that it sleeps at least the given amount, but not that it continues immediately after that.
Offline Herkules

Senior Devvie

Friendly fire isn't friendly!

« Reply #2 - Posted 2002-12-29 09:08:10 »

There's no way just with the JDK. Java3D utils timer does the job, but need Java3D.

Depending on your distribution needs (legal issues), you can easily isolate the timing stuff from Java3D. There remains 1 dll and one source file. But this it is not legal to distribute them w/o Java3D....

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline whome

Junior Devvie

Carte Noir Java

« Reply #3 - Posted 2003-01-22 10:02:35 »

See this article about JNI timer. Maybe it gives you some ideas.
Offline Riven

« JGO Overlord »

Medals: 1341
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #4 - Posted 2004-08-02 06:07:38 »

FYI: 0.1ms = 100,000ns, not 100ns

1,000,000ns per 1ms

You probably won't need 100ns accuracy.

Edit: ,.,.,.,.,. Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline blahblahblahh

JGO Coder

Medals: 1

« Reply #5 - Posted 2004-08-02 11:04:29 » Skippy from a strange continent Wink where they use "." instead of ","?

Surely 0.1ms != 100.000 ns?

Either 0.1ms == 100,000 ns or 0,1 ms == 100.000 ns ?

Anyway, to OP: can you wait until java 1.5 goes gold? It has a nanosecond timer...

malloc will be first against the wall when the revolution comes...
Offline jbanes

JGO Coder

Projects: 1

"Java Games? Incredible! Mr. Incredible, that is!"

« Reply #6 - Posted 2004-08-02 12:07:27 »

Remind me to chew MarkusKidd out sometime for recommending the sleeper hack. Not only is it not accurate, but it's likely to get you into extremely hot water.

If you need a Hi-Res timer, you can try GAGETimer. It gives nanosecond precision on Java 1.5, access to the Hi-Res Multimedia Timer on Windows (via JNI), 1ms precision on other platforms, and a reasonable attempt to allow for good timing on Windows if the DLL is missing. If you need access to a hi-res timer on other platforms, you can always extend it via the "NativeTimer" interface.

I don't know if that will solve your problem, but it might be an option worth checking into. My only warning is to make sure you calculate the number of ticks instead of assuming their resolution. Multimedia timers on Windows are notorious for having different resolutions on machines with a different number of processors.

Edit: Nevermind on sacking MarkusKidd. I hadn't realized that someone had revived a two year old thread. I think the original poster has probably figured something out by now. Smiley

Java Game Console Project
Last Journal Entry: 12/17/04
Pages: [1]
  ignore  |  Print  

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

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

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

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

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

nelsongames (156 views)
2018-04-24 18:14:32

ivj94 (897 views)
2018-03-24 14:47:39

ivj94 (161 views)
2018-03-24 14:46:31

ivj94 (810 views)
2018-03-24 14:43:53

Solater (174 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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!