author | psadhukhan |
Mon, 16 Nov 2015 10:56:21 +0300 | |
changeset 34397 | 86a74cb6c903 |
parent 32865 | f9cb6e427f9e |
child 37550 | c8252b8fea3d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31653
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
2 |
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
||
28 |
package javax.swing; |
|
29 |
||
30 |
||
31 |
||
29922 | 32 |
import java.security.AccessController; |
33 |
import java.security.PrivilegedAction; |
|
2 | 34 |
import java.util.*; |
35 |
import java.util.concurrent.*; |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
36 |
import java.util.concurrent.locks.*; |
2 | 37 |
import java.util.concurrent.atomic.AtomicLong; |
38 |
import sun.awt.AppContext; |
|
31653
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
39 |
import sun.misc.ManagedLocalsThread; |
2 | 40 |
|
41 |
/** |
|
42 |
* Internal class to manage all Timers using one thread. |
|
43 |
* TimerQueue manages a queue of Timers. The Timers are chained |
|
44 |
* together in a linked list sorted by the order in which they will expire. |
|
45 |
* |
|
46 |
* @author Dave Moore |
|
47 |
* @author Igor Kushnirskiy |
|
48 |
*/ |
|
49 |
class TimerQueue implements Runnable |
|
50 |
{ |
|
51 |
private static final Object sharedInstanceKey = |
|
52 |
new StringBuffer("TimerQueue.sharedInstanceKey"); |
|
53 |
private static final Object expiredTimersKey = |
|
54 |
new StringBuffer("TimerQueue.expiredTimersKey"); |
|
55 |
||
56 |
private final DelayQueue<DelayedTimer> queue; |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
57 |
private volatile boolean running; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
58 |
private final Lock runningLock; |
2 | 59 |
|
60 |
/* Lock object used in place of class object for synchronization. |
|
61 |
* (4187686) |
|
62 |
*/ |
|
63 |
private static final Object classLock = new Object(); |
|
64 |
||
65 |
/** Base of nanosecond timings, to avoid wrapping */ |
|
66 |
private static final long NANO_ORIGIN = System.nanoTime(); |
|
67 |
||
68 |
/** |
|
69 |
* Constructor for TimerQueue. |
|
70 |
*/ |
|
71 |
public TimerQueue() { |
|
72 |
super(); |
|
73 |
queue = new DelayQueue<DelayedTimer>(); |
|
74 |
// Now start the TimerQueue thread. |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
75 |
runningLock = new ReentrantLock(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
76 |
startIfNeeded(); |
2 | 77 |
} |
78 |
||
79 |
||
80 |
public static TimerQueue sharedInstance() { |
|
81 |
synchronized (classLock) { |
|
82 |
TimerQueue sharedInst = (TimerQueue) |
|
83 |
SwingUtilities.appContextGet( |
|
84 |
sharedInstanceKey); |
|
85 |
if (sharedInst == null) { |
|
86 |
sharedInst = new TimerQueue(); |
|
87 |
SwingUtilities.appContextPut(sharedInstanceKey, sharedInst); |
|
88 |
} |
|
89 |
return sharedInst; |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
||
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
94 |
void startIfNeeded() { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
95 |
if (! running) { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
96 |
runningLock.lock(); |
32120
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
97 |
if (running) { |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
98 |
return; |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
99 |
} |
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
100 |
try { |
29922 | 101 |
final ThreadGroup threadGroup = AppContext.getAppContext().getThreadGroup(); |
102 |
AccessController.doPrivileged((PrivilegedAction<Object>) () -> { |
|
103 |
String name = "TimerQueue"; |
|
31653
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
104 |
Thread timerThread = new ManagedLocalsThread(threadGroup, |
d88ff422c7fb
8080405: Exception in thread "AWT-EventQueue-1" java.security.AccessControlException
serb
parents:
29922
diff
changeset
|
105 |
this, name); |
29922 | 106 |
timerThread.setDaemon(true); |
107 |
timerThread.setPriority(Thread.NORM_PRIORITY); |
|
108 |
timerThread.start(); |
|
109 |
return null; |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
110 |
}); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
111 |
running = true; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
112 |
} finally { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
113 |
runningLock.unlock(); |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
114 |
} |
2 | 115 |
} |
116 |
} |
|
117 |
||
118 |
void addTimer(Timer timer, long delayMillis) { |
|
119 |
timer.getLock().lock(); |
|
120 |
try { |
|
121 |
// If the Timer is already in the queue, then ignore the add. |
|
122 |
if (! containsTimer(timer)) { |
|
123 |
addTimer(new DelayedTimer(timer, |
|
124 |
TimeUnit.MILLISECONDS.toNanos(delayMillis) |
|
125 |
+ now())); |
|
126 |
} |
|
127 |
} finally { |
|
128 |
timer.getLock().unlock(); |
|
129 |
} |
|
130 |
} |
|
131 |
||
132 |
private void addTimer(DelayedTimer delayedTimer) { |
|
133 |
assert delayedTimer != null && ! containsTimer(delayedTimer.getTimer()); |
|
134 |
||
135 |
Timer timer = delayedTimer.getTimer(); |
|
136 |
timer.getLock().lock(); |
|
137 |
try { |
|
138 |
timer.delayedTimer = delayedTimer; |
|
139 |
queue.add(delayedTimer); |
|
140 |
} finally { |
|
141 |
timer.getLock().unlock(); |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
void removeTimer(Timer timer) { |
|
146 |
timer.getLock().lock(); |
|
147 |
try { |
|
148 |
if (timer.delayedTimer != null) { |
|
149 |
queue.remove(timer.delayedTimer); |
|
150 |
timer.delayedTimer = null; |
|
151 |
} |
|
152 |
} finally { |
|
153 |
timer.getLock().unlock(); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
boolean containsTimer(Timer timer) { |
|
158 |
timer.getLock().lock(); |
|
159 |
try { |
|
160 |
return timer.delayedTimer != null; |
|
161 |
} finally { |
|
162 |
timer.getLock().unlock(); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
||
167 |
public void run() { |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
168 |
runningLock.lock(); |
2 | 169 |
try { |
170 |
while (running) { |
|
171 |
try { |
|
32120
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
172 |
DelayedTimer runningTimer = queue.take(); |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
173 |
Timer timer = runningTimer.getTimer(); |
2 | 174 |
timer.getLock().lock(); |
175 |
try { |
|
176 |
DelayedTimer delayedTimer = timer.delayedTimer; |
|
32120
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
177 |
if (delayedTimer == runningTimer) { |
2 | 178 |
/* |
32120
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
179 |
* Timer is not removed (delayedTimer != null) |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
180 |
* or not removed and added (runningTimer == delayedTimer) |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
181 |
* after we get it from the queue and before the |
06c83c5f2912
8130735: javax.swing.TimerQueue: timer fires late when another timer starts
ssadetsky
parents:
31653
diff
changeset
|
182 |
* lock on the timer is acquired |
2 | 183 |
*/ |
184 |
timer.post(); // have timer post an event |
|
185 |
timer.delayedTimer = null; |
|
186 |
if (timer.isRepeats()) { |
|
187 |
delayedTimer.setTime(now() |
|
188 |
+ TimeUnit.MILLISECONDS.toNanos( |
|
189 |
timer.getDelay())); |
|
190 |
addTimer(delayedTimer); |
|
191 |
} |
|
192 |
} |
|
13352
a9ef04068234
7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents:
5506
diff
changeset
|
193 |
|
a9ef04068234
7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents:
5506
diff
changeset
|
194 |
// Allow run other threads on systems without kernel threads |
a9ef04068234
7167780: Hang javasoft.sqe.tests.api.javax.swing.Timer.Ctor2Tests
rupashka
parents:
5506
diff
changeset
|
195 |
timer.getLock().newCondition().awaitNanos(1); |
2 | 196 |
} catch (SecurityException ignore) { |
197 |
} finally { |
|
198 |
timer.getLock().unlock(); |
|
199 |
} |
|
2485
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
200 |
} catch (InterruptedException ie) { |
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
201 |
// Shouldn't ignore InterruptedExceptions here, so AppContext |
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
202 |
// is disposed gracefully, see 6799345 for details |
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
203 |
if (AppContext.getAppContext().isDisposed()) { |
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
204 |
break; |
dc7165666847
6799345: JFC demos threw exception in the Java Console when applets are closed
art
parents:
1301
diff
changeset
|
205 |
} |
2 | 206 |
} |
207 |
} |
|
208 |
} |
|
209 |
catch (ThreadDeath td) { |
|
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
210 |
// 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
|
211 |
for (DelayedTimer delayedTimer : queue) { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
212 |
delayedTimer.getTimer().cancelEvent(); |
2 | 213 |
} |
1273
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
214 |
throw td; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
215 |
} finally { |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
216 |
running = false; |
3b2eba521268
6623943: javax.swing.TimerQueue's thread occasionally fails to start
idk
parents:
2
diff
changeset
|
217 |
runningLock.unlock(); |
2 | 218 |
} |
219 |
} |
|
220 |
||
221 |
||
222 |
public String toString() { |
|
223 |
StringBuilder buf = new StringBuilder(); |
|
224 |
buf.append("TimerQueue ("); |
|
225 |
boolean isFirst = true; |
|
226 |
for (DelayedTimer delayedTimer : queue) { |
|
227 |
if (! isFirst) { |
|
228 |
buf.append(", "); |
|
229 |
} |
|
230 |
buf.append(delayedTimer.getTimer().toString()); |
|
231 |
isFirst = false; |
|
232 |
} |
|
233 |
buf.append(")"); |
|
234 |
return buf.toString(); |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Returns nanosecond time offset by origin |
|
239 |
*/ |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
1273
diff
changeset
|
240 |
private static long now() { |
2 | 241 |
return System.nanoTime() - NANO_ORIGIN; |
242 |
} |
|
243 |
||
244 |
static class DelayedTimer implements Delayed { |
|
245 |
// most of it copied from |
|
246 |
// java.util.concurrent.ScheduledThreadPoolExecutor |
|
247 |
||
248 |
/** |
|
249 |
* Sequence number to break scheduling ties, and in turn to |
|
250 |
* guarantee FIFO order among tied entries. |
|
251 |
*/ |
|
252 |
private static final AtomicLong sequencer = new AtomicLong(0); |
|
253 |
||
254 |
/** Sequence number to break ties FIFO */ |
|
255 |
private final long sequenceNumber; |
|
256 |
||
257 |
||
258 |
/** The time the task is enabled to execute in nanoTime units */ |
|
259 |
private volatile long time; |
|
260 |
||
261 |
private final Timer timer; |
|
262 |
||
263 |
DelayedTimer(Timer timer, long nanos) { |
|
264 |
this.timer = timer; |
|
265 |
time = nanos; |
|
266 |
sequenceNumber = sequencer.getAndIncrement(); |
|
267 |
} |
|
268 |
||
269 |
||
32865
f9cb6e427f9e
8136783: Run blessed-modifier-order script on java.desktop
prr
parents:
32120
diff
changeset
|
270 |
public final long getDelay(TimeUnit unit) { |
2 | 271 |
return unit.convert(time - now(), TimeUnit.NANOSECONDS); |
272 |
} |
|
273 |
||
274 |
final void setTime(long nanos) { |
|
275 |
time = nanos; |
|
276 |
} |
|
277 |
||
278 |
final Timer getTimer() { |
|
279 |
return timer; |
|
280 |
} |
|
281 |
||
282 |
public int compareTo(Delayed other) { |
|
283 |
if (other == this) { // compare zero ONLY if same object |
|
284 |
return 0; |
|
285 |
} |
|
286 |
if (other instanceof DelayedTimer) { |
|
287 |
DelayedTimer x = (DelayedTimer)other; |
|
288 |
long diff = time - x.time; |
|
289 |
if (diff < 0) { |
|
290 |
return -1; |
|
291 |
} else if (diff > 0) { |
|
292 |
return 1; |
|
293 |
} else if (sequenceNumber < x.sequenceNumber) { |
|
294 |
return -1; |
|
295 |
} else { |
|
296 |
return 1; |
|
297 |
} |
|
298 |
} |
|
299 |
long d = (getDelay(TimeUnit.NANOSECONDS) - |
|
300 |
other.getDelay(TimeUnit.NANOSECONDS)); |
|
301 |
return (d == 0) ? 0 : ((d < 0) ? -1 : 1); |
|
302 |
} |
|
303 |
} |
|
304 |
} |