This example run a background task repeatly in 5 seconds to print system time.
|
Example of using java.util.Timer |
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.util.Date;
/**
* @web helloraspberrypi.blogspot.com
*/
class testTimer{
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask timeTask = new TimerTask() {
@Override
public void run() {
System.out.println((new Date()).toString());
}
};
timer.schedule(timeTask, 0, 5000);
System.out.println("Press ENTER to exit");
try{
System.in.read();
}catch(IOException ex){
System.out.println(ex.toString());
}
System.out.println("-BYE-");
timer.cancel();
}
}
No comments:
Post a Comment