public class Timer extends Thread { private int interval; private ITimerListener listener; public Timer() { } public Timer(int interval, ITimerListener listener) { this.interval = interval; this.listener = listener; } @Override public void run() { while (true) { listener.timerCall(); try { sleep(interval); } catch (InterruptedException ex) { ex.printStackTrace(); } } } public ITimerListener getListener() { return listener; } public void setListener(ITimerListener listener) { this.listener = listener; } public int getInterval() { return interval; } public void setInterval(int interval) { this.interval = interval; } }
ITimerListener interface
public interface ITimerListener { public void timerCall(); }