Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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 4124 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 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
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.
http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 849
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
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2004-08-02 11:04:29 »

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

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

CopyableCougar4 (13 views)
2014-12-28 02:10:29

BurntPizza (15 views)
2014-12-27 22:38:51

Mr.CodeIt (13 views)
2014-12-27 04:03:04

TheDudeFromCI (17 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (86 views)
2014-12-05 12:39:02
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
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!