Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java timing question (possible solution?)

Started by Big Sassy Oct 10, 2002 at 4:33 PM 4 replies 1k views
Original Post
Big Sassy
Big Sassy
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:
  
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;
}  
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]
WayfarerX
WayfarerX
quote:
Original post by Big Sassy
1. I only have an XP machine to test this function on. Is it that accurate on all machines (9x/unix/etc)?


Timer uses Object.wait(long) underneith, so it can vary betweem platforms and environemnts.

quote:
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?


Read the docs. Each class has a tag called "Since" indicating what version of Java it was introduced. java.util.Timer was introduced in 1.3.

quote:
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?


First, remember that the VM can swap threads whenever it wants, so it may pause your main loop to execute your timer. Also: docs again.
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Big Sassy
Big Sassy
Thanks for answering my questions so far. I should have looked at the docs before posting. A few more questions though:

quote:
Original post by WayfarerX
Timer uses Object.wait(long) underneith, so it can vary betweem platforms and environemnts.

Would you know how much it varies between different platforms? I''m presuming it doesn''t use the timer used in GetTickCount(). I know the resolution of that timer for different systems (10ms NT, 50+ms for 9x, 1ms unix). Does this timer use a high resolution timer (like the one used in QueryPerformanceCounter in C/C++)?

quote:
Read the docs. Each class has a tag called "Since" indicating what version of Java it was introduced. java.util.Timer was introduced in 1.3.


Ok. But what about any changes that may have happened to it since 1.3? If any changes did happen, would the docs list them, or would I find them somewhere else?

I didn''t find the answers to those two questions in the docs. I really appreciate the help so far. Please keep it coming

***********************
          
WayfarerX
WayfarerX
quote:
Original post by Big Sassy
Would you know how much it varies between different platforms? ... Does this timer use a high resolution timer (like the one used in QueryPerformanceCounter in C/C++)?

I don''t know off hand what the variation is like, but I do know there is no high res timer in the core Java API.
quote:
Ok. But what about any changes that may have happened to it since 1.3? If any changes did happen, would the docs list them, or would I find them somewhere else?

Methods may also have a "Since" tag if they are added to a class after the class was introduced to the API. On Sun''s site they have the Javadocs for all versions of Java, so you could compare and contrast between them if you need to.
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Big Sassy
Big Sassy
Ok. Thanks a lot WayfarerX. You're alright

EDIT - Just remembered that you helped me out before here. In case your wondering, this has nothing to do with that. This is just for a project I'm working on for class. Thanks for all the help.

***********************
          

[edited by - Big Sassy on October 10, 2002 9:19:45 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.