1 7 8 9 10 package javax.swing; 11 12 13 14 import java.util.*; 15 import sun.awt.AppContext; 16 17 18 19 27 class TimerQueue implements Runnable 28 { 29 private static final Object sharedInstanceKey = 30 new StringBuffer ("TimerQueue.sharedInstanceKey"); 31 private static final Object expiredTimersKey = 32 new StringBuffer ("TimerQueue.expiredTimersKey"); 33 34 Timer firstTimer; 35 boolean running; 36 37 40 private static final Object classLock = new Object (); 41 42 43 46 public TimerQueue() { 47 super(); 48 49 start(); 51 } 52 53 54 public static TimerQueue sharedInstance() { 55 synchronized (classLock) { 56 TimerQueue sharedInst = (TimerQueue ) 57 SwingUtilities.appContextGet( 58 sharedInstanceKey); 59 if (sharedInst == null) { 60 sharedInst = new TimerQueue (); 61 SwingUtilities.appContextPut(sharedInstanceKey, sharedInst); 62 } 63 return sharedInst; 64 } 65 } 66 67 68 synchronized void start() { 69 if (running) { 70 throw new RuntimeException ("Can't start a TimerQueue " + 71 "that is already running"); 72 } 73 else { 74 final ThreadGroup threadGroup = 75 AppContext.getAppContext().getThreadGroup(); 76 java.security.AccessController.doPrivileged( 77 new java.security.PrivilegedAction () { 78 public Object run() { 79 Thread timerThread = new Thread (threadGroup, TimerQueue.this, 80 "TimerQueue"); 81 timerThread.setDaemon(true); 82 timerThread.setPriority(Thread.NORM_PRIORITY); 83 timerThread.start(); 84 return null; 85 } 86 }); 87 running = true; 88 } 89 } 90 91 92 synchronized void stop() { 93 running = false; 94 notify(); 95 } 96 97 98 synchronized void addTimer(Timer timer, long expirationTime) { 99 Timer previousTimer; 100 Timer nextTimer; 101 102 if (timer.running) { 104 return; 105 } 106 107 previousTimer = null; 108 nextTimer = firstTimer; 109 110 114 while (nextTimer != null) { 115 if (nextTimer.expirationTime > expirationTime) break; 116 117 previousTimer = nextTimer; 118 nextTimer = nextTimer.nextTimer; 119 } 120 121 if (previousTimer == null) { 122 firstTimer = timer; 123 } 124 else { 125 previousTimer.nextTimer = timer; 126 } 127 128 timer.expirationTime = expirationTime; 129 timer.nextTimer = nextTimer; 130 timer.running = true; 131 notify(); 132 } 133 134 135 synchronized void removeTimer(Timer timer) { 136 Timer previousTimer; 137 Timer nextTimer; 138 boolean found; 139 140 if (!timer.running) return; 141 142 previousTimer = null; 143 nextTimer = firstTimer; 144 found = false; 145 146 while (nextTimer != null) { 147 if (nextTimer == timer) { 148 found = true; 149 break; 150 } 151 152 previousTimer = nextTimer; 153 nextTimer = nextTimer.nextTimer; 154 } 155 156 if (!found) return; 157 158 if (previousTimer == null) { 159 firstTimer = timer.nextTimer; 160 } 161 else { 162 previousTimer.nextTimer = timer.nextTimer; 163 } 164 165 timer.expirationTime = 0; 166 timer.nextTimer = null; 167 timer.running = false; 168 } 169 170 171 synchronized boolean containsTimer(Timer timer) { 172 return timer.running; 173 } 174 175 176 181 synchronized long postExpiredTimers() { 182 Timer timer; 183 long currentTime; 184 long timeToWait; 185 186 189 do { 190 timer = firstTimer; 191 if (timer == null) return 0; 192 193 currentTime = System.currentTimeMillis(); 194 timeToWait = timer.expirationTime - currentTime; 195 196 if (timeToWait <= 0) { 197 try { 198 timer.post(); } 200 catch (SecurityException e) { 201 } 202 203 removeTimer(timer); 205 206 if (timer.isRepeats()) { 209 addTimer(timer, currentTime + timer.getDelay()); 210 } 211 212 217 try { 218 wait(1); 219 } 220 catch (InterruptedException e) { 221 } 222 } 223 } while (timeToWait <= 0); 224 225 return timeToWait; 226 } 227 228 229 public synchronized void run() { 230 long timeToWait; 231 232 try { 233 while (running) { 234 timeToWait = postExpiredTimers(); 235 try { 236 wait(timeToWait); 237 } 238 catch (InterruptedException e) { 239 } 240 } 241 } 242 catch (ThreadDeath td) { 243 running = false; 244 Timer timer = firstTimer; 246 while (timer != null) { 247 timer.cancelEvent(); 248 timer = timer.nextTimer; 249 } 250 SystemEventQueueUtilities.restartTimerQueueThread(); 251 throw td; 252 } 253 } 254 255 256 public synchronized String toString() { 257 StringBuffer buf; 258 Timer nextTimer; 259 260 buf = new StringBuffer (); 261 buf.append("TimerQueue ("); 262 263 nextTimer = firstTimer; 264 while (nextTimer != null) { 265 buf.append(nextTimer.toString()); 266 267 nextTimer = nextTimer.nextTimer; 268 if (nextTimer != null) buf.append(", "); 269 } 270 271 buf.append(")"); 272 return buf.toString(); 273 } 274 } 275 | Popular Tags |