HighResTimer class on the wiki?

I registered for the wiki to post the hirestimer class from Snowmoon that we all know and love, but I gather I have to provide hosting for the file. I’ve searched the new forums with several word combinations but couldn’t find it. Can be no one has reposted it?!?! I don’t have a host besides geocities as of yet, so here it is;


 public class HighResTimer implements Runnable 
{
      private int ticks, autoLength;      
      private Thread timerThread;
      private boolean autoCorrection = true;
      private boolean running = false;
      private long timeDiff, startTime, endTime, msDelay, targetMs;
      
      public HighResTimer() {
            
            timerThread = new Thread(this);
            timerThread.setPriority(Thread.MAX_PRIORITY);
            timerThread.setDaemon(true);
      }
      
      public int getTickCount() {
            return ticks;
      }
      
      public void resetTickCount() {
            ticks = 0;      
      }
      
      public void startTimer() {
            running = true;
            timerThread.start();
      }
      
      public void stopTimer() {
            
            running = false;
            try {
                  timerThread.join();
            }
            catch (Exception e) {}
      }
      
      public void setAutoCorrection(boolean on, int sampleLength) {
      
            autoCorrection = on;
            autoLength = sampleLength;
      }
      
      public void setDelay(int tmsDelay) {
      
            msDelay = tmsDelay;
            targetMs = msDelay;
      }
      
      public void run() {
      
            startTime = System.currentTimeMillis();
            try {
                  while (running) {   
                        if (msDelay > 0)
                        Thread.sleep(msDelay);
                        ticks++;
                        synchronized (this) {
                              notifyAll();
                        }
                        if (autoCorrection && (ticks % autoLength == 0)) {
                              endTime = System.currentTimeMillis();
                              timeDiff = ((endTime - startTime) / autoLength) - targetMs;
                              startTime = endTime;
                              if (timeDiff > 0) {
                                    msDelay--;
                              }
                              if (timeDiff < 0) {
                                    msDelay++;
                              }
                        }
                  }
            }
            catch (Exception e) {}
      }
}  


A number of people are using the one in the com.sun.* packages temporarily until 1.5, at which point there will be one in the java.* standard APIs.

Jeff, could you tell me the resolution of that timer that will be a standard Java inclusion in 1.5?

I’ve been following the high-res timer debate for many months, but I always prefer to use standard tools, if they are of sufficient quality.

[quote]Jeff, could you tell me the resolution of that timer that will be a standard Java inclusion in 1.5?
[…]
[/quote]

[quote]Okay, i got this change. Thank Josh Block and Doug Lea:

"We replaced first sentence of nanoTime spec with:

 * Returns the current value of the most precise available system
 * timer, in nanoseconds.

Which ought to have the right effect."

Its now required to be the best precision possible. If its not, and you can show a way to get higher precision, you can log it as a bug against the VM :slight_smile: (Ours or anyone elses :wink: )

JK
[/quote]
:slight_smile:

Thanks.