The java.util.Timer class provides an alternative way to perform scheduled or recurrent tasks.
import java.util.Timer;
import java.util.TimerTask;
public class MainClass {
public static void main(String[] args) {
Timer timer = new Timer();
timer.schedule(new DisplayQuestionTask(), 0, 10 * 1000);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
}
timer.cancel();
}
}
class DisplayQuestionTask extends TimerTask {
int counter = 0;
public void run() {
System.out.println(counter++);
}
}