|
1 /* |
|
2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 |
|
27 |
|
28 package javax.swing; |
|
29 |
|
30 |
|
31 |
|
32 import java.util.*; |
|
33 import java.util.concurrent.*; |
|
34 import java.util.concurrent.atomic.AtomicLong; |
|
35 import sun.awt.AppContext; |
|
36 |
|
37 |
|
38 |
|
39 /** |
|
40 * Internal class to manage all Timers using one thread. |
|
41 * TimerQueue manages a queue of Timers. The Timers are chained |
|
42 * together in a linked list sorted by the order in which they will expire. |
|
43 * |
|
44 * @author Dave Moore |
|
45 * @author Igor Kushnirskiy |
|
46 */ |
|
47 class TimerQueue implements Runnable |
|
48 { |
|
49 private static final Object sharedInstanceKey = |
|
50 new StringBuffer("TimerQueue.sharedInstanceKey"); |
|
51 private static final Object expiredTimersKey = |
|
52 new StringBuffer("TimerQueue.expiredTimersKey"); |
|
53 |
|
54 private final DelayQueue<DelayedTimer> queue; |
|
55 volatile boolean running; |
|
56 |
|
57 /* Lock object used in place of class object for synchronization. |
|
58 * (4187686) |
|
59 */ |
|
60 private static final Object classLock = new Object(); |
|
61 |
|
62 /** Base of nanosecond timings, to avoid wrapping */ |
|
63 private static final long NANO_ORIGIN = System.nanoTime(); |
|
64 |
|
65 /** |
|
66 * Constructor for TimerQueue. |
|
67 */ |
|
68 public TimerQueue() { |
|
69 super(); |
|
70 queue = new DelayQueue<DelayedTimer>(); |
|
71 // Now start the TimerQueue thread. |
|
72 start(); |
|
73 } |
|
74 |
|
75 |
|
76 public static TimerQueue sharedInstance() { |
|
77 synchronized (classLock) { |
|
78 TimerQueue sharedInst = (TimerQueue) |
|
79 SwingUtilities.appContextGet( |
|
80 sharedInstanceKey); |
|
81 if (sharedInst == null) { |
|
82 sharedInst = new TimerQueue(); |
|
83 SwingUtilities.appContextPut(sharedInstanceKey, sharedInst); |
|
84 } |
|
85 return sharedInst; |
|
86 } |
|
87 } |
|
88 |
|
89 |
|
90 synchronized void start() { |
|
91 if (running) { |
|
92 throw new RuntimeException("Can't start a TimerQueue " + |
|
93 "that is already running"); |
|
94 } |
|
95 else { |
|
96 final ThreadGroup threadGroup = |
|
97 AppContext.getAppContext().getThreadGroup(); |
|
98 java.security.AccessController.doPrivileged( |
|
99 new java.security.PrivilegedAction() { |
|
100 public Object run() { |
|
101 Thread timerThread = new Thread(threadGroup, TimerQueue.this, |
|
102 "TimerQueue"); |
|
103 timerThread.setDaemon(true); |
|
104 timerThread.setPriority(Thread.NORM_PRIORITY); |
|
105 timerThread.start(); |
|
106 return null; |
|
107 } |
|
108 }); |
|
109 running = true; |
|
110 } |
|
111 } |
|
112 |
|
113 synchronized void stop() { |
|
114 running = false; |
|
115 } |
|
116 |
|
117 void addTimer(Timer timer, long delayMillis) { |
|
118 timer.getLock().lock(); |
|
119 try { |
|
120 // If the Timer is already in the queue, then ignore the add. |
|
121 if (! containsTimer(timer)) { |
|
122 addTimer(new DelayedTimer(timer, |
|
123 TimeUnit.MILLISECONDS.toNanos(delayMillis) |
|
124 + now())); |
|
125 } |
|
126 } finally { |
|
127 timer.getLock().unlock(); |
|
128 } |
|
129 } |
|
130 |
|
131 private void addTimer(DelayedTimer delayedTimer) { |
|
132 assert delayedTimer != null && ! containsTimer(delayedTimer.getTimer()); |
|
133 |
|
134 Timer timer = delayedTimer.getTimer(); |
|
135 timer.getLock().lock(); |
|
136 try { |
|
137 timer.delayedTimer = delayedTimer; |
|
138 queue.add(delayedTimer); |
|
139 } finally { |
|
140 timer.getLock().unlock(); |
|
141 } |
|
142 } |
|
143 |
|
144 void removeTimer(Timer timer) { |
|
145 timer.getLock().lock(); |
|
146 try { |
|
147 if (timer.delayedTimer != null) { |
|
148 queue.remove(timer.delayedTimer); |
|
149 timer.delayedTimer = null; |
|
150 } |
|
151 } finally { |
|
152 timer.getLock().unlock(); |
|
153 } |
|
154 } |
|
155 |
|
156 boolean containsTimer(Timer timer) { |
|
157 timer.getLock().lock(); |
|
158 try { |
|
159 return timer.delayedTimer != null; |
|
160 } finally { |
|
161 timer.getLock().unlock(); |
|
162 } |
|
163 } |
|
164 |
|
165 |
|
166 public void run() { |
|
167 try { |
|
168 while (running) { |
|
169 try { |
|
170 Timer timer = queue.take().getTimer(); |
|
171 timer.getLock().lock(); |
|
172 try { |
|
173 DelayedTimer delayedTimer = timer.delayedTimer; |
|
174 if (delayedTimer != null) { |
|
175 /* |
|
176 * Timer is not removed after we get it from |
|
177 * the queue and before the lock on the timer is |
|
178 * acquired |
|
179 */ |
|
180 timer.post(); // have timer post an event |
|
181 timer.delayedTimer = null; |
|
182 if (timer.isRepeats()) { |
|
183 delayedTimer.setTime(now() |
|
184 + TimeUnit.MILLISECONDS.toNanos( |
|
185 timer.getDelay())); |
|
186 addTimer(delayedTimer); |
|
187 } |
|
188 } |
|
189 } catch (SecurityException ignore) { |
|
190 } finally { |
|
191 timer.getLock().unlock(); |
|
192 } |
|
193 } catch (InterruptedException ignore) { |
|
194 } |
|
195 } |
|
196 } |
|
197 catch (ThreadDeath td) { |
|
198 synchronized (this) { |
|
199 running = false; |
|
200 // Mark all the timers we contain as not being queued. |
|
201 for (DelayedTimer delayedTimer : queue) { |
|
202 delayedTimer.getTimer().cancelEvent(); |
|
203 } |
|
204 throw td; |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 |
|
210 public String toString() { |
|
211 StringBuilder buf = new StringBuilder(); |
|
212 buf.append("TimerQueue ("); |
|
213 boolean isFirst = true; |
|
214 for (DelayedTimer delayedTimer : queue) { |
|
215 if (! isFirst) { |
|
216 buf.append(", "); |
|
217 } |
|
218 buf.append(delayedTimer.getTimer().toString()); |
|
219 isFirst = false; |
|
220 } |
|
221 buf.append(")"); |
|
222 return buf.toString(); |
|
223 } |
|
224 |
|
225 /** |
|
226 * Returns nanosecond time offset by origin |
|
227 */ |
|
228 private final static long now() { |
|
229 return System.nanoTime() - NANO_ORIGIN; |
|
230 } |
|
231 |
|
232 static class DelayedTimer implements Delayed { |
|
233 // most of it copied from |
|
234 // java.util.concurrent.ScheduledThreadPoolExecutor |
|
235 |
|
236 /** |
|
237 * Sequence number to break scheduling ties, and in turn to |
|
238 * guarantee FIFO order among tied entries. |
|
239 */ |
|
240 private static final AtomicLong sequencer = new AtomicLong(0); |
|
241 |
|
242 /** Sequence number to break ties FIFO */ |
|
243 private final long sequenceNumber; |
|
244 |
|
245 |
|
246 /** The time the task is enabled to execute in nanoTime units */ |
|
247 private volatile long time; |
|
248 |
|
249 private final Timer timer; |
|
250 |
|
251 DelayedTimer(Timer timer, long nanos) { |
|
252 this.timer = timer; |
|
253 time = nanos; |
|
254 sequenceNumber = sequencer.getAndIncrement(); |
|
255 } |
|
256 |
|
257 |
|
258 final public long getDelay(TimeUnit unit) { |
|
259 return unit.convert(time - now(), TimeUnit.NANOSECONDS); |
|
260 } |
|
261 |
|
262 final void setTime(long nanos) { |
|
263 time = nanos; |
|
264 } |
|
265 |
|
266 final Timer getTimer() { |
|
267 return timer; |
|
268 } |
|
269 |
|
270 public int compareTo(Delayed other) { |
|
271 if (other == this) { // compare zero ONLY if same object |
|
272 return 0; |
|
273 } |
|
274 if (other instanceof DelayedTimer) { |
|
275 DelayedTimer x = (DelayedTimer)other; |
|
276 long diff = time - x.time; |
|
277 if (diff < 0) { |
|
278 return -1; |
|
279 } else if (diff > 0) { |
|
280 return 1; |
|
281 } else if (sequenceNumber < x.sequenceNumber) { |
|
282 return -1; |
|
283 } else { |
|
284 return 1; |
|
285 } |
|
286 } |
|
287 long d = (getDelay(TimeUnit.NANOSECONDS) - |
|
288 other.getDelay(TimeUnit.NANOSECONDS)); |
|
289 return (d == 0) ? 0 : ((d < 0) ? -1 : 1); |
|
290 } |
|
291 } |
|
292 } |