import java.util.Timer;
import java.util.TimerTask;
...
/**
* set up an event to happen later
* @param delayInMillis how long to wait
*/
public void doLater( long delayInMillis )
{
new Timer().schedule( new ScheduleRunner(), delayInMillis );
new Timer().schedule( new ScheduleRunner(), delayInMillis , delayInMillis );
}
/**
* run method will be invoked when time is up.
*/
class ScheduleRunner extends TimerTask
{
/**
* executed when time is up.
*/
public void run()
{
doSomething();
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
private static ScheduledExecutorService scheduler;
...
scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate( new Runnable()
{
public void run()
{
doSomething();
}
}, 5, 5, TimeUnit.SECONDS );
...
scheduler.shutdownNow();