> how does this differ from just calling sleep()?
How does what differ, Mike?
<http://www.physci.org/codes/javafaq.jsp#netiquette>
> i've been told ..you need to use a timer
> instead of just calling sleep().. cuz sleep() doesnt guarantee it sleeps for
> exactly x amount of time.. so i apparently need a timer to ensure this.. but
> does the swing timer work the same way as the one in the link?..
Check for yourself.
<sscce>
// To gain smooth time based animation, the renderer needs
// to calculate the positions of the visible elements at the
// time of painting the Graphics object.
import java.awt.event.*;
import javax.swing.Timer;
import java.util.Date;
/** This example demonstrates the time drift of both a
Thread and a javax.swing.Timer set for '1 second' delay.
Using Java 1.5.0 beta, the Timer drifts off time faster
than the Thread.
@author Andrew Thompson */
public class TimeDrifter implements Runnable, ActionListener {
/** The Timer used in this example */
Timer timer;
/** Time started. */
long timerStart, threadStart;
/** Duration of test in seconds (give or take 1). */
final int DURATION = 120;
/** Compile true for more detailed output
but less acccurate results. */
final boolean TEST = false;
TimeDrifter() {
//timer.addActionListener(this);
Thread t = new Thread(this);
System.out.println("Test duration: " + DURATION + " seconds." );
System.out.println("Start thread test" );
threadStart = System.currentTimeMillis();
t.start();
}
public void timerStart() {
timer = new Timer(1000, this);
System.out.println("Start timer test" );
timerStart = System.currentTimeMillis();
timer.start();
}
/** Starts a new Thread to sleep for 1000 ms. */
public void run() {
while (true) {
try {
float timeInSec =
(System.currentTimeMillis()-threadStart)/1000f;
if (TEST) System.out.println("thread: \t" +
(timeInSec-(int)timeInSec) );
if ( (int)timeInSec>DURATION ) {
System.out.println("thread drift:\t " +
(timeInSec-(int)timeInSec) +
" / over: " + (int)timeInSec + " seconds" );
timerStart();
Thread.sleep(DURATION*2*1000);
} else {
Thread.sleep(1000);
}
} catch (InterruptedException ie) {
// ignore
}
}
}
public void actionPerformed(ActionEvent ae) {
float timeInSec =
(System.currentTimeMillis()-timerStart)/1000f;
if (TEST) System.out.println("timer: \t" +
(timeInSec-(int)timeInSec) );
if ( (int)timeInSec>DURATION ) {
timer.stop();
System.out.println("timer drift: \t " +
(timeInSec-(int)timeInSec) +
" / over: " + (int)timeInSec + "seconds" );
//((Runnable)this).Thread.interrupt();
System.exit(0);
}
}
public static void main(String[] args) {
new TimeDrifter();
}
}
</sscce>
Run 1;
Test duration: 120 seconds.
Start thread test
thread drift: 0.04399872 / over: 121 seconds
Start timer test
timer drift: 0.4850006 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=4
4+485&width=80>
Run 2:
Test duration: 120 seconds.
Start thread test
thread drift: 0.125 / over: 121 seconds
Start timer test
timer drift: 0.22399902 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=1
25+223&width=80>
Run 3:
Test duration: 120 seconds.
Start thread test
thread drift: 0.033996582 / over: 121 seconds
Start timer test
timer drift: 0.20400238 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=3
4+204&width=80>
Run 4:
Test duration: 120 seconds.
Start thread test
thread drift: 0.024002075 / over: 121 seconds
Start timer test
timer drift: 0.2539978 / over: 121seconds
<http://www.physci.org/test/graph/index.jsp?nm=Drift+in+ms&lbl=Thread+Timer&val=2
4+254&width=80>
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane