improving java's Timer and TimerTask

hello,

java has a nice bundle of classes to schedule execution. namely java.util.Timer and java.util.TimerTask. unfortunately they work with currentTimeMillis() which is just not accurate enough for realtime games.

so i was trying to replace currentTimeMillis() with nanoTime(), but now my schedulded tasks are executed once and then never again.
i was already looking through relevant code if there are any precision errors like a division or a conversion from milliseconds to seconds which would need adaption of course. but i couldnt find anything.

does anyone know why java.util.Timer does not work anymore when replacing millis with nanos?

thanks!

depending on what you need it for: https://timingframework.dev.java.net/

System.currentTimeMillis() is in milli-seconds
System.nanoTime() is in nano-seconds.

So simply replacing one with the other - like you stated - is almost correct.

thanks, yeah, that’s why i was looking for code which could be specific to nanos or millis, which could be the cause for the repeating execution not working.

i’m still stuck with this problem… does anyone have an idea what i could be doing wrong? thanks!

maybe try to use a “proxy” class as timer that call System.currentTimeMillis to keep some trace of what happen than do the same with nanosecond timer. it may give you some informations on what cause this issue ?

thanks for the info.

i am now doing a testcase to debug it further. seems there is an Object.wait() somewhere which takes milliseconds as arguments.

still it is not working very well… i gotta look into this some more deeper.


public static long milliTime()
{
   return System.nanoTime() / 1000000L;
}

?