   [java] System.currentTimeMillis() unreliable?
  I run the script below and compare it to a reliable timer.  On some machines it runs fine - in sync with the timer. But on some machines the clock below runs faster than the timer! The OS on all machines are Win2K and JRE 1.4.2.  I really need to fix this problem, any input would be great. public void run () {long startTime=System.currentTimeMillis();long currentSeconds;long currentMinutes;while (engineRunning) {try {sleep(100);}catch(InterruptedException e) {}currentSeconds = (System.currentTimeMillis()-startTime)/1000;currentMinutes = currentSeconds/60;currentSeconds = currentSeconds-(currentMinutes*60);System.out.println(currentMinutes+":"+currentSeconds);}} 
  Hi Spyder,this is just a shot in the dark, but try to print the stack trace of the InterruptedException. Suppose there really _is_ an interruption to your sleep(100), then your timer would run faster, since the code below is executed more frequently. So check if there is such an exception. If this should be the case, you'd have a clue on where to start.Otherwise, I don't have any ideas and the above could be completely off.Good luck though,BerenOooups: Edit: Jeeee --- forget it --- that was something COMPLETELY off -- sorry sorry sorry...No idea then.   [edited by - Beren77 on July 31, 2003 7:44:15 PM];  Thanks for a reply.The problem is not the speed of my timer. The problem is that this line of code:System.currentTimeMillis()-startTimeproduces huge differences in seconds when compared to a reliable timer.Example. My screen reads "13 minutes 3 seconds" with code based on System.currentTimeMillis(). My stopwatch reads "12 mintures 33 seconds".    [edited by - spyder on July 31, 2003 8:11:11 PM];  You mean like a physical, seperate stopwatch? I''d compare it to a native Windows timer. It''s entirely possible your stopwatch is really slow or your computer''s really fast. But if CTM''s is inline with your computer''s clock, then you can''t really lay the blame on CTM. Grab the Gage timer, http://java.dnsalias.com/, and set up a small test comparing CTM with gage. Gage uses window''s performance counter. CTM''s resolution is only 10 millis on Win2K, so it causes a lot of "stair stepping" effects, but I''ve never heard of it "drifting".Even so, CTM stinks for games. Unless you''re going Unix only, you''ll want to use something like the Gage timer because CTM''s resolution''s so horrible (it''s friggin 50-60 millis on Win9x). ;  > You mean like a physical, seperate stopwatch? Yes. The stopwatch is in sync with a majority of computers so I take its accurate. Two machines with the drifting problem are out of sync with the stopwatch.I found this problem when having game clients connect to a java game server. The clients and server have different speeds on the CTM causing unexpected performance!!> if CTM''s is inline with your computer''s clock, then you can''t really lay the blame on CTM. The strange thing is that the CTM appears way off (drifting), but the computer clock is running correctly. How do I explain that?;  GAGE did the trick! Any idea why the CTM is so odd anyways? ;  Yeah I suggest you check out the GAGE implementation which is very accurate since it uses the native timer. Go to http://www.javagaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1059523907for the answer and a link to the GAGE timer   [edited by - wolfbane on August 4, 2003 10:15:51 AM]