Original Post
I know questions about high res timers in Java come up often. And I've seen a lot of different answers. But has anyone notices that scheduleAtFixedRate() has a resolution of 1ms? Please call me a dumbass if that is common knowledge. Anyway, if this is true I have a few questions:
1. I only have an XP machine to test this function on. Is it that accurate on all machines (9x/unix/etc)?
2. I'm using version 1.4.1. of the JavaSDK. Does this function not exist in older versions? If it does exists in older versions, is it as accurate?
3. Let's say I have the Timer call a TimerTask object every 5ms. Now let's say I have a main loop (in another thread) that takes 23ms to run through the loop on one iteration. I have a call to sleep(0) at the end of the loop. Will the TimerTask thread then run 4 times to catch up (23ms / 5ms ~= 4 times), or would it run once and then control will go back to the main thread? Here's an example of what the code would look like:
If anybody can answer ANY of my questions I would truly appreciate it. Oh, and I'm still pretty newbish when it comes to Java so please be gentle on me if I deserve flames for this
edit - typos
[edited by - Big Sassy on October 10, 2002 5:38:40 PM]
import java.util.*;
public class MainProgram
{
public static void main(String[] args)
{
timer = new Timer(true); // create timer with daemon thread
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
// run something here every 5ms
}
},
0, 5); // 0 = delay before thread starts
// 5 = miliseconds between calls
while(true) {
// main game loop
// pretend one iteration took 23ms
sleep(0);
}
timer.cancel();
}
private Timer timer;
}