Any way to get near 100th nanosecond precision?

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

http://forum.java.sun.com/thread.jsp?forum=406&thread=340391&tstart=0&trange=15

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)

~Jeffrey

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.

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…

See this article about JNI timer. Maybe it gives you some ideas.
http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html?

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

1,000,000ns per 1ms

You probably won’t need 100ns accuracy.

Edit: ,.,.,.,.,. :slight_smile:

Um…is 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…

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. :slight_smile: