1 29 package jegg.timer; 30 31 import java.util.TimerTask ; 32 33 37 public final class Timer 38 { 39 42 43 private long _interval; 44 45 private long _delay; 46 47 private boolean _repeat; 48 49 private static final java.util.Timer TIMER = new java.util.Timer (true ); 50 51 private MyTimerTask _task; 52 53 59 public static final Timer createRepeatingTimer(TimeoutListener l, long period) 60 { 61 return new Timer(l, period, period, true); 62 } 63 64 72 public static final Timer createRepeatingTimer(TimeoutListener l, long period, long delay) 73 { 74 return new Timer(l, period, delay, true); 75 } 76 77 84 public static final Timer createSingleShotTimer(TimeoutListener l, long delay) 85 { 86 return new Timer(l, delay, delay, false); 87 } 88 89 96 private Timer(TimeoutListener tl, long msec, long delay_msec, boolean r) 97 { 98 _interval = msec; 99 _delay = delay_msec; 100 _repeat = r; 101 _task = new MyTimerTask(tl, this, _repeat); 102 TIMER.schedule( _task, _delay, _interval); 103 } 104 105 110 public final long getInterval() 111 { 112 return _interval; 113 } 114 115 119 public final long getDelay() 120 { 121 return _delay; 122 } 123 124 129 public final boolean isRepeating() 130 { 131 return _repeat; 132 } 133 134 138 public final void cancel() 139 { 140 _task.cancel(); 141 } 142 143 147 private final class MyTimerTask extends TimerTask 148 { 149 private TimeoutListener _tl; 150 private Timer _t; 151 private boolean _r; 152 public MyTimerTask(TimeoutListener tl, Timer t, boolean isRepeating) 153 { 154 _tl = tl; 155 _t = t; 156 _r = isRepeating; 157 } 158 public void run() 159 { 160 _tl.timeout(_t); 161 if (!_r) 162 { 163 cancel(); 164 } 165 } 166 } 167 } 168 | Popular Tags |