lejos.utility.Timer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lejos-ev3-api Show documentation
Show all versions of lejos-ev3-api Show documentation
leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.
The newest version!
package lejos.utility;
/**
* Timer object, with some similar functionality to java.Swing.Timer.
*
* @author Ryan VanderBijl
*/
public class Timer
{
private TimerListener myListener;
private Thread myThread ;
private int delay ;
private boolean running ;
/**
* Create a Timer object. Every theDelay milliseconds
* the el.timedOut() function is called. You may
* change the delay with setDelay(int). You need
* to call start() explicitly.
*/
public Timer(int theDelay, TimerListener el)
{
running = false;
delay = theDelay;
myListener = el;
myThread = new Thread() {
public void run() {
int d;
boolean r;
while(true) {
synchronized(Timer.this)
{
d = delay;
r = running;
}
if (r)
{
Delay.msDelay(d);
myListener.timedOut();
} else {
yield();
}
}
}
};
myThread.setDaemon(true);
}
/**
* access how man milliseconds between timedOut() messages.
*/
public synchronized int getDelay() {
return delay;
}
/**
* Change the delay between timedOut messages. Safe to call
* while start()ed. Time in milli-seconds.
*/
public synchronized void setDelay(int newDelay) {
delay = newDelay;
}
/**
* Stops the timer. timedOut() messages are not sent.
*/
public synchronized void stop() {
running = false;
}
/**
* Starts the timer, telling it to send timeOut() methods
* to the TimerListener.
*/
public synchronized void start() {
running = true;
if (!myThread.isAlive())
myThread.start();
}
}