Thursday, March 19, 2009

How to execute code at regular interval?

The java.util.Timer class can be used to schedule code to be executed repeated at a fixed interval. The TimerTask class is used to encapsulate the code to be executed.


Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
// put the code you want to run here
}
}, initialDelay, interval);

No comments: