author | rupashka |
Tue, 26 Aug 2008 15:12:54 +0400 | |
changeset 1301 | 15e81207e1f2 |
parent 1273 | 3b2eba521268 |
child 1639 | a97859015238 |
child 2485 | dc7165666847 |
permissions | -rw-r--r-- |
2 | 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.*; |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
34 |
import java.util.concurrent.locks.*; |
2 | 35 |
import java.util.concurrent.atomic.AtomicLong; |
36 |
import sun.awt.AppContext; |
|
37 |
||
38 |
||
39 |
||
40 |
/** |
|
41 |
* Internal class to manage all Timers using one thread. |
|
42 |
* TimerQueue manages a queue of Timers. The Timers are chained |
|
43 |
* together in a linked list sorted by the order in which they will expire. |
|
44 |
* |
|
45 |
* @author Dave Moore |
|
46 |
* @author Igor Kushnirskiy |
|
47 |
*/ |
|
48 |
class TimerQueue implements Runnable |
|
49 |
{ |
|
50 |
private static final Object sharedInstanceKey = |
|
51 |
new StringBuffer("TimerQueue.sharedInstanceKey"); |
|
52 |
private static final Object expiredTimersKey = |
|
53 |
new StringBuffer("TimerQueue.expiredTimersKey"); |
|
54 |
||
55 |
private final DelayQueue<DelayedTimer> queue; |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
56 |
private volatile boolean running; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
57 |
private final Lock runningLock; |
2 | 58 |
|
59 |
/* Lock object used in place of class object for synchronization. |
|
60 |
* (4187686) |
|
61 |
*/ |
|
62 |
private static final Object classLock = new Object(); |
|
63 |
||
64 |
/** Base of nanosecond timings, to avoid wrapping */ |
|
65 |
private static final long NANO_ORIGIN = System.nanoTime(); |
|
66 |
||
67 |
/** |
|
68 |
* Constructor for TimerQueue. |
|
69 |
*/ |
|
70 |
public TimerQueue() { |
|
71 |
super(); |
|
72 |
queue = new DelayQueue<DelayedTimer>(); |
|
73 |
// Now start the TimerQueue thread. |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
74 |
runningLock = new ReentrantLock(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
75 |
startIfNeeded(); |
2 | 76 |
} |
77 |
||
78 |
||
79 |
public static TimerQueue sharedInstance() { |
|
80 |
synchronized (classLock) { |
|
81 |
TimerQueue sharedInst = (TimerQueue) |
|
82 |
SwingUtilities.appContextGet( |
|
83 |
sharedInstanceKey); |
|
84 |
if (sharedInst == null) { |
|
85 |
sharedInst = new TimerQueue(); |
|
86 |
SwingUtilities.appContextPut(sharedInstanceKey, sharedInst); |
|
87 |
} |
|
88 |
return sharedInst; |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
||
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
93 |
void startIfNeeded() { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
94 |
if (! running) { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
95 |
runningLock.lock(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
96 |
try { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
97 |
final ThreadGroup threadGroup = |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
98 |
AppContext.getAppContext().getThreadGroup(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
99 |
java.security.AccessController.doPrivileged( |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
1273
diff
changeset
|
100 |
new java.security.PrivilegedAction<Object>() { |
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
101 |
public Object run() { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
102 |
Thread timerThread = new Thread(threadGroup, TimerQueue.this, |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
103 |
"TimerQueue"); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
104 |
timerThread.setDaemon(true); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
105 |
timerThread.setPriority(Thread.NORM_PRIORITY); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
106 |
timerThread.start(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
107 |
return null; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
108 |
} |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
109 |
}); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
110 |
running = true; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
111 |
} finally { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
112 |
runningLock.unlock(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
113 |
} |
2 | 114 |
} |
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() { |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
167 |
runningLock.lock(); |
2 | 168 |
try { |
169 |
while (running) { |
|
170 |
try { |
|
171 |
Timer timer = queue.take().getTimer(); |
|
172 |
timer.getLock().lock(); |
|
173 |
try { |
|
174 |
DelayedTimer delayedTimer = timer.delayedTimer; |
|
175 |
if (delayedTimer != null) { |
|
176 |
/* |
|
177 |
* Timer is not removed after we get it from |
|
178 |
* the queue and before the lock on the timer is |
|
179 |
* acquired |
|
180 |
*/ |
|
181 |
timer.post(); // have timer post an event |
|
182 |
timer.delayedTimer = null; |
|
183 |
if (timer.isRepeats()) { |
|
184 |
delayedTimer.setTime(now() |
|
185 |
+ TimeUnit.MILLISECONDS.toNanos( |
|
186 |
timer.getDelay())); |
|
187 |
addTimer(delayedTimer); |
|
188 |
} |
|
189 |
} |
|
190 |
} catch (SecurityException ignore) { |
|
191 |
} finally { |
|
192 |
timer.getLock().unlock(); |
|
193 |
} |
|
194 |
} catch (InterruptedException ignore) { |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
catch (ThreadDeath td) { |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
199 |
// Mark all the timers we contain as not being queued. |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
200 |
for (DelayedTimer delayedTimer : queue) { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
201 |
delayedTimer.getTimer().cancelEvent(); |
2 | 202 |
} |
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
203 |
throw td; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
204 |
} finally { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
205 |
running = false; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
206 |
runningLock.unlock(); |
2 | 207 |
} |
208 |
} |
|
209 |
||
210 |
||
211 |
public String toString() { |
|
212 |
StringBuilder buf = new StringBuilder(); |
|
213 |
buf.append("TimerQueue ("); |
|
214 |
boolean isFirst = true; |
|
215 |
for (DelayedTimer delayedTimer : queue) { |
|
216 |
if (! isFirst) { |
|
217 |
buf.append(", "); |
|
218 |
} |
|
219 |
buf.append(delayedTimer.getTimer().toString()); |
|
220 |
isFirst = false; |
|
221 |
} |
|
222 |
buf.append(")"); |
|
223 |
return buf.toString(); |
|
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* Returns nanosecond time offset by origin |
|
228 |
*/ |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
1273
diff
changeset
|
229 |
private static long now() { |
2 | 230 |
return System.nanoTime() - NANO_ORIGIN; |
231 |
} |
|
232 |
||
233 |
static class DelayedTimer implements Delayed { |
|
234 |
// most of it copied from |
|
235 |
// java.util.concurrent.ScheduledThreadPoolExecutor |
|
236 |
||
237 |
/** |
|
238 |
* Sequence number to break scheduling ties, and in turn to |
|
239 |
* guarantee FIFO order among tied entries. |
|
240 |
*/ |
|
241 |
private static final AtomicLong sequencer = new AtomicLong(0); |
|
242 |
||
243 |
/** Sequence number to break ties FIFO */ |
|
244 |
private final long sequenceNumber; |
|
245 |
||
246 |
||
247 |
/** The time the task is enabled to execute in nanoTime units */ |
|
248 |
private volatile long time; |
|
249 |
||
250 |
private final Timer timer; |
|
251 |
||
252 |
DelayedTimer(Timer timer, long nanos) { |
|
253 |
this.timer = timer; |
|
254 |
time = nanos; |
|
255 |
sequenceNumber = sequencer.getAndIncrement(); |
|
256 |
} |
|
257 |
||
258 |
||
259 |
final public long getDelay(TimeUnit unit) { |
|
260 |
return unit.convert(time - now(), TimeUnit.NANOSECONDS); |
|
261 |
} |
|
262 |
||
263 |
final void setTime(long nanos) { |
|
264 |
time = nanos; |
|
265 |
} |
|
266 |
||
267 |
final Timer getTimer() { |
|
268 |
return timer; |
|
269 |
} |
|
270 |
||
271 |
public int compareTo(Delayed other) { |
|
272 |
if (other == this) { // compare zero ONLY if same object |
|
273 |
return 0; |
|
274 |
} |
|
275 |
if (other instanceof DelayedTimer) { |
|
276 |
DelayedTimer x = (DelayedTimer)other; |
|
277 |
long diff = time - x.time; |
|
278 |
if (diff < 0) { |
|
279 |
return -1; |
|
280 |
} else if (diff > 0) { |
|
281 |
return 1; |
|
282 |
} else if (sequenceNumber < x.sequenceNumber) { |
|
283 |
return -1; |
|
284 |
} else { |
|
285 |
return 1; |
|
286 |
} |
|
287 |
} |
|
288 |
long d = (getDelay(TimeUnit.NANOSECONDS) - |
|
289 |
other.getDelay(TimeUnit.NANOSECONDS)); |
|
290 |
return (d == 0) ? 0 : ((d < 0) ? -1 : 1); |
|
291 |
} |
|
292 |
} |
|
293 |
} |