1 14 package org.wings.util; 15 16 22 23 27 public final class TimerQueue 28 implements Runnable { 29 32 private static TimerQueue sharedInstance = new TimerQueue(); 33 34 private Timer firstTimer; 35 private boolean running; 36 37 40 private TimerQueue() { 41 super(); 42 43 start(); 45 } 46 47 50 public static TimerQueue sharedInstance() { 51 return sharedInstance; 52 } 53 54 private synchronized void start() { 55 System.err.println("Timer started"); 56 if (running) { 57 throw new RuntimeException ("Can't start a TimerQueue that is already running"); 58 } else { 59 Thread timerThread = new Thread (this, "TimerQueue"); 60 61 try { 62 timerThread.setDaemon(true); 63 } catch (SecurityException e) {} 64 timerThread.start(); 65 running = true; 66 } 67 } 68 69 private synchronized void stop() { 70 running = false; 71 notifyAll(); 72 } 73 74 public synchronized void addTimer(Timer timer, long expirationTime) { 75 Timer previousTimer, nextTimer; 76 77 if (timer.running) { 79 return; 80 } 81 82 previousTimer = null; 83 nextTimer = firstTimer; 84 85 89 while (nextTimer != null) { 90 if (nextTimer.expirationTime > expirationTime) 91 break; 92 93 previousTimer = nextTimer; 94 nextTimer = nextTimer.nextTimer; 95 } 96 97 if (previousTimer == null) 98 firstTimer = timer; 99 else 100 previousTimer.nextTimer = timer; 101 102 timer.expirationTime = expirationTime; 103 timer.nextTimer = nextTimer; 104 timer.running = true; 105 notifyAll(); 106 } 107 108 public synchronized void removeTimer(Timer timer) { 109 boolean found; 110 Timer previousTimer, nextTimer; 111 112 if (!timer.running) 113 return; 114 115 previousTimer = null; 116 nextTimer = firstTimer; 117 found = false; 118 119 while (nextTimer != null) { 120 if (nextTimer == timer) { 121 found = true; 122 break; 123 } 124 125 previousTimer = nextTimer; 126 nextTimer = nextTimer.nextTimer; 127 } 128 129 if (!found) 130 return; 131 132 if (previousTimer == null) 133 firstTimer = timer.nextTimer; 134 else 135 previousTimer.nextTimer = timer.nextTimer; 136 137 timer.expirationTime = 0; 138 timer.nextTimer = null; 139 timer.running = false; 140 } 141 142 public synchronized boolean containsTimer(Timer timer) { 143 return timer.running; 144 } 145 146 150 public synchronized long postExpiredTimers() { 151 long currentTime, timeToWait; 152 Timer timer; 153 154 157 do { 158 timer = firstTimer; 159 if (timer == null) 160 return 0; 161 162 currentTime = System.currentTimeMillis(); 163 timeToWait = timer.expirationTime - currentTime; 164 165 if (timeToWait <= 0) { 166 try { 167 timer.post(); } catch (SecurityException e) {} 169 170 removeTimer(timer); 172 173 if (timer.isRepeats()) { 176 addTimer(timer, currentTime + timer.getDelay()); 177 } 178 } 179 180 185 try { 186 wait(1); 187 } catch (InterruptedException e) { 188 } 189 } while (timeToWait <= 0); 190 191 return timeToWait; 192 } 193 194 public synchronized void run() { 195 long timeToWait; 196 197 try { 198 while (running) { 199 timeToWait = postExpiredTimers(); 200 try { 201 wait(timeToWait); 202 } catch (InterruptedException e) { } 203 } 204 } catch (ThreadDeath td) { 205 running = false; 206 Timer timer = firstTimer; 208 while (timer != null) { 209 timer.eventQueued = false; 210 timer = timer.nextTimer; 211 } 212 sharedInstance.start(); 213 throw td; 214 } 215 } 216 217 public synchronized String toString() { 218 StringBuffer buf; 219 Timer nextTimer; 220 221 buf = new StringBuffer (); 222 buf.append("TimerQueue ("); 223 224 nextTimer = firstTimer; 225 while (nextTimer != null) { 226 buf.append(nextTimer.toString()); 227 228 nextTimer = nextTimer.nextTimer; 229 if (nextTimer != null) 230 buf.append(", "); 231 } 232 233 buf.append(")"); 234 return buf.toString(); 235 } 236 } 237 238 239 | Popular Tags |