1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/licenses/publicdomain |
5 |
|
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
import junit.framework.*; |
11 |
|
|
import java.util.*; |
12 |
|
|
import java.util.concurrent.*; |
13 |
|
|
import java.util.concurrent.locks.*; |
14 |
|
|
import java.io.*; |
15 |
|
|
|
16 |
|
|
public class AbstractQueuedSynchronizerTest extends JSR166TestCase { |
17 |
|
|
public static void main(String[] args) { |
18 |
|
|
junit.textui.TestRunner.run (suite()); |
19 |
|
|
} |
20 |
|
|
public static Test suite() { |
21 |
|
|
return new TestSuite(AbstractQueuedSynchronizerTest.class); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
/** |
25 |
dl |
1.12 |
* A simple mutex class, adapted from the |
26 |
|
|
* AbstractQueuedSynchronizer javadoc. Exclusive acquire tests |
27 |
|
|
* exercise this as a sample user extension. Other |
28 |
|
|
* methods/features of AbstractQueuedSynchronizerTest are tested |
29 |
|
|
* via other test classes, including those for ReentrantLock, |
30 |
|
|
* ReentrantReadWriteLock, and Semaphore |
31 |
|
|
*/ |
32 |
|
|
static class Mutex extends AbstractQueuedSynchronizer { |
33 |
dl |
1.16 |
public boolean isHeldExclusively() { return getState() == 1; } |
34 |
dl |
1.12 |
|
35 |
dl |
1.16 |
public boolean tryAcquire(int acquires) { |
36 |
dl |
1.12 |
assertTrue(acquires == 1); |
37 |
|
|
return compareAndSetState(0, 1); |
38 |
|
|
} |
39 |
|
|
|
40 |
dl |
1.16 |
public boolean tryRelease(int releases) { |
41 |
|
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
42 |
dl |
1.12 |
setState(0); |
43 |
|
|
return true; |
44 |
|
|
} |
45 |
|
|
|
46 |
dl |
1.15 |
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
47 |
dl |
1.12 |
|
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* A simple latch class, to test shared mode. |
53 |
|
|
*/ |
54 |
|
|
static class BooleanLatch extends AbstractQueuedSynchronizer { |
55 |
|
|
public boolean isSignalled() { return getState() != 0; } |
56 |
|
|
|
57 |
|
|
public int tryAcquireShared(int ignore) { |
58 |
|
|
return isSignalled()? 1 : -1; |
59 |
dl |
1.1 |
} |
60 |
dl |
1.12 |
|
61 |
|
|
public boolean tryReleaseShared(int ignore) { |
62 |
|
|
setState(1); |
63 |
|
|
return true; |
64 |
dl |
1.1 |
} |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
dl |
1.16 |
* A runnable calling acquireInterruptibly |
69 |
dl |
1.1 |
*/ |
70 |
dl |
1.17 |
class InterruptibleSyncRunnable implements Runnable { |
71 |
|
|
final Mutex sync; |
72 |
|
|
InterruptibleSyncRunnable(Mutex l) { sync = l; } |
73 |
dl |
1.1 |
public void run() { |
74 |
|
|
try { |
75 |
dl |
1.17 |
sync.acquireInterruptibly(1); |
76 |
dl |
1.1 |
} catch(InterruptedException success){} |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/** |
82 |
dl |
1.16 |
* A runnable calling acquireInterruptibly that expects to be |
83 |
dl |
1.1 |
* interrupted |
84 |
|
|
*/ |
85 |
dl |
1.17 |
class InterruptedSyncRunnable implements Runnable { |
86 |
|
|
final Mutex sync; |
87 |
|
|
InterruptedSyncRunnable(Mutex l) { sync = l; } |
88 |
dl |
1.1 |
public void run() { |
89 |
|
|
try { |
90 |
dl |
1.17 |
sync.acquireInterruptibly(1); |
91 |
dl |
1.1 |
threadShouldThrow(); |
92 |
|
|
} catch(InterruptedException success){} |
93 |
|
|
} |
94 |
|
|
} |
95 |
dl |
1.16 |
|
96 |
|
|
/** |
97 |
|
|
* isHeldExclusively is false upon construction |
98 |
|
|
*/ |
99 |
|
|
public void testIsHeldExclusively() { |
100 |
|
|
Mutex rl = new Mutex(); |
101 |
|
|
assertFalse(rl.isHeldExclusively()); |
102 |
|
|
} |
103 |
dl |
1.1 |
|
104 |
|
|
/** |
105 |
dl |
1.17 |
* acquiring released sync succeeds |
106 |
dl |
1.1 |
*/ |
107 |
dl |
1.16 |
public void testAcquire() { |
108 |
dl |
1.1 |
Mutex rl = new Mutex(); |
109 |
dl |
1.16 |
rl.acquire(1); |
110 |
|
|
assertTrue(rl.isHeldExclusively()); |
111 |
|
|
rl.release(1); |
112 |
dl |
1.17 |
assertFalse(rl.isHeldExclusively()); |
113 |
dl |
1.1 |
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
dl |
1.17 |
* tryAcquire on an released sync succeeds |
117 |
dl |
1.1 |
*/ |
118 |
dl |
1.16 |
public void testTryAcquire() { |
119 |
dl |
1.1 |
Mutex rl = new Mutex(); |
120 |
dl |
1.16 |
assertTrue(rl.tryAcquire(1)); |
121 |
|
|
assertTrue(rl.isHeldExclusively()); |
122 |
|
|
rl.release(1); |
123 |
dl |
1.1 |
} |
124 |
|
|
|
125 |
|
|
/** |
126 |
|
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
|
*/ |
128 |
|
|
public void testhasQueuedThreads() { |
129 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
130 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
132 |
dl |
1.1 |
try { |
133 |
dl |
1.17 |
assertFalse(sync.hasQueuedThreads()); |
134 |
|
|
sync.acquire(1); |
135 |
dl |
1.1 |
t1.start(); |
136 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
137 |
dl |
1.17 |
assertTrue(sync.hasQueuedThreads()); |
138 |
dl |
1.1 |
t2.start(); |
139 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
140 |
dl |
1.17 |
assertTrue(sync.hasQueuedThreads()); |
141 |
dl |
1.1 |
t1.interrupt(); |
142 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
143 |
dl |
1.17 |
assertTrue(sync.hasQueuedThreads()); |
144 |
|
|
sync.release(1); |
145 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
146 |
dl |
1.17 |
assertFalse(sync.hasQueuedThreads()); |
147 |
dl |
1.10 |
t1.join(); |
148 |
|
|
t2.join(); |
149 |
|
|
} catch(Exception e){ |
150 |
|
|
unexpectedException(); |
151 |
|
|
} |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
/** |
155 |
dl |
1.11 |
* isQueued(null) throws NPE |
156 |
|
|
*/ |
157 |
|
|
public void testIsQueuedNPE() { |
158 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
159 |
dl |
1.11 |
try { |
160 |
dl |
1.17 |
sync.isQueued(null); |
161 |
dl |
1.11 |
shouldThrow(); |
162 |
|
|
} catch (NullPointerException success) { |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
/** |
167 |
dl |
1.10 |
* isQueued reports whether a thread is queued. |
168 |
|
|
*/ |
169 |
|
|
public void testIsQueued() { |
170 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
171 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
172 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
173 |
|
|
try { |
174 |
|
|
assertFalse(sync.isQueued(t1)); |
175 |
|
|
assertFalse(sync.isQueued(t2)); |
176 |
|
|
sync.acquire(1); |
177 |
dl |
1.10 |
t1.start(); |
178 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
179 |
dl |
1.17 |
assertTrue(sync.isQueued(t1)); |
180 |
dl |
1.10 |
t2.start(); |
181 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
182 |
dl |
1.17 |
assertTrue(sync.isQueued(t1)); |
183 |
|
|
assertTrue(sync.isQueued(t2)); |
184 |
dl |
1.10 |
t1.interrupt(); |
185 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
186 |
dl |
1.17 |
assertFalse(sync.isQueued(t1)); |
187 |
|
|
assertTrue(sync.isQueued(t2)); |
188 |
|
|
sync.release(1); |
189 |
dl |
1.10 |
Thread.sleep(SHORT_DELAY_MS); |
190 |
dl |
1.17 |
assertFalse(sync.isQueued(t1)); |
191 |
|
|
assertFalse(sync.isQueued(t2)); |
192 |
dl |
1.8 |
t1.join(); |
193 |
|
|
t2.join(); |
194 |
|
|
} catch(Exception e){ |
195 |
|
|
unexpectedException(); |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
|
199 |
dl |
1.12 |
/** |
200 |
dl |
1.17 |
* getFirstQueuedThread returns first waiting thread or null if none |
201 |
dl |
1.12 |
*/ |
202 |
|
|
public void testGetFirstQueuedThread() { |
203 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
204 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
205 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
206 |
dl |
1.12 |
try { |
207 |
dl |
1.17 |
assertNull(sync.getFirstQueuedThread()); |
208 |
|
|
sync.acquire(1); |
209 |
dl |
1.12 |
t1.start(); |
210 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
211 |
dl |
1.17 |
assertEquals(t1, sync.getFirstQueuedThread()); |
212 |
dl |
1.12 |
t2.start(); |
213 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
214 |
dl |
1.17 |
assertEquals(t1, sync.getFirstQueuedThread()); |
215 |
dl |
1.12 |
t1.interrupt(); |
216 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
217 |
dl |
1.17 |
assertEquals(t2, sync.getFirstQueuedThread()); |
218 |
|
|
sync.release(1); |
219 |
dl |
1.12 |
Thread.sleep(SHORT_DELAY_MS); |
220 |
dl |
1.17 |
assertNull(sync.getFirstQueuedThread()); |
221 |
dl |
1.12 |
t1.join(); |
222 |
|
|
t2.join(); |
223 |
|
|
} catch(Exception e){ |
224 |
|
|
unexpectedException(); |
225 |
|
|
} |
226 |
|
|
} |
227 |
|
|
|
228 |
dl |
1.8 |
|
229 |
|
|
/** |
230 |
dl |
1.12 |
* hasContended reports false if no thread has ever blocked, else true |
231 |
dl |
1.8 |
*/ |
232 |
dl |
1.12 |
public void testHasContended() { |
233 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
234 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
235 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
236 |
dl |
1.8 |
try { |
237 |
dl |
1.17 |
assertFalse(sync.hasContended()); |
238 |
|
|
sync.acquire(1); |
239 |
|
|
t1.start(); |
240 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
241 |
|
|
assertTrue(sync.hasContended()); |
242 |
|
|
t2.start(); |
243 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
244 |
|
|
assertTrue(sync.hasContended()); |
245 |
|
|
t1.interrupt(); |
246 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
247 |
|
|
assertTrue(sync.hasContended()); |
248 |
|
|
sync.release(1); |
249 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
250 |
|
|
assertTrue(sync.hasContended()); |
251 |
|
|
t1.join(); |
252 |
|
|
t2.join(); |
253 |
|
|
} catch(Exception e){ |
254 |
|
|
unexpectedException(); |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
/** |
259 |
|
|
* getQueuedThreads includes waiting threads |
260 |
|
|
*/ |
261 |
|
|
public void testGetQueuedThreads() { |
262 |
|
|
final Mutex sync = new Mutex(); |
263 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
264 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
265 |
|
|
try { |
266 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
267 |
|
|
sync.acquire(1); |
268 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
|
|
t1.start(); |
270 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
271 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
272 |
|
|
t2.start(); |
273 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
274 |
|
|
assertTrue(sync.getQueuedThreads().contains(t1)); |
275 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
276 |
|
|
t1.interrupt(); |
277 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
278 |
|
|
assertFalse(sync.getQueuedThreads().contains(t1)); |
279 |
|
|
assertTrue(sync.getQueuedThreads().contains(t2)); |
280 |
|
|
sync.release(1); |
281 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
282 |
|
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
283 |
|
|
t1.join(); |
284 |
|
|
t2.join(); |
285 |
|
|
} catch(Exception e){ |
286 |
|
|
unexpectedException(); |
287 |
|
|
} |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
/** |
291 |
|
|
* getExclusiveQueuedThreads includes waiting threads |
292 |
|
|
*/ |
293 |
|
|
public void testGetExclusiveQueuedThreads() { |
294 |
|
|
final Mutex sync = new Mutex(); |
295 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
296 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
297 |
|
|
try { |
298 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
299 |
|
|
sync.acquire(1); |
300 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
301 |
|
|
t1.start(); |
302 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
303 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
304 |
|
|
t2.start(); |
305 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
306 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t1)); |
307 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
308 |
|
|
t1.interrupt(); |
309 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
310 |
|
|
assertFalse(sync.getExclusiveQueuedThreads().contains(t1)); |
311 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().contains(t2)); |
312 |
|
|
sync.release(1); |
313 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
314 |
|
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
315 |
|
|
t1.join(); |
316 |
|
|
t2.join(); |
317 |
|
|
} catch(Exception e){ |
318 |
|
|
unexpectedException(); |
319 |
|
|
} |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
/** |
323 |
|
|
* getSharedQueuedThreads does not include exclusively waiting threads |
324 |
|
|
*/ |
325 |
|
|
public void testGetSharedQueuedThreads() { |
326 |
|
|
final Mutex sync = new Mutex(); |
327 |
|
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
328 |
|
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
329 |
|
|
try { |
330 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
331 |
|
|
sync.acquire(1); |
332 |
|
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
333 |
dl |
1.8 |
t1.start(); |
334 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
335 |
dl |
1.17 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
336 |
dl |
1.8 |
t2.start(); |
337 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
338 |
dl |
1.17 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
339 |
dl |
1.8 |
t1.interrupt(); |
340 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
341 |
dl |
1.17 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
342 |
|
|
sync.release(1); |
343 |
dl |
1.8 |
Thread.sleep(SHORT_DELAY_MS); |
344 |
dl |
1.17 |
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
345 |
dl |
1.1 |
t1.join(); |
346 |
|
|
t2.join(); |
347 |
|
|
} catch(Exception e){ |
348 |
|
|
unexpectedException(); |
349 |
|
|
} |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
/** |
353 |
dl |
1.16 |
* tryAcquireNanos is interruptible. |
354 |
dl |
1.1 |
*/ |
355 |
|
|
public void testInterruptedException2() { |
356 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
357 |
|
|
sync.acquire(1); |
358 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
359 |
|
|
public void run() { |
360 |
|
|
try { |
361 |
dl |
1.17 |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
362 |
dl |
1.1 |
threadShouldThrow(); |
363 |
|
|
} catch(InterruptedException success){} |
364 |
|
|
} |
365 |
|
|
}); |
366 |
|
|
try { |
367 |
|
|
t.start(); |
368 |
|
|
t.interrupt(); |
369 |
|
|
} catch(Exception e){ |
370 |
|
|
unexpectedException(); |
371 |
|
|
} |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
|
375 |
|
|
/** |
376 |
dl |
1.17 |
* TryAcquire on exclusively held sync fails |
377 |
dl |
1.1 |
*/ |
378 |
dl |
1.17 |
public void testTryAcquireWhenSynced() { |
379 |
|
|
final Mutex sync = new Mutex(); |
380 |
|
|
sync.acquire(1); |
381 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
382 |
|
|
public void run() { |
383 |
dl |
1.17 |
threadAssertFalse(sync.tryAcquire(1)); |
384 |
dl |
1.1 |
} |
385 |
|
|
}); |
386 |
|
|
try { |
387 |
|
|
t.start(); |
388 |
|
|
t.join(); |
389 |
dl |
1.17 |
sync.release(1); |
390 |
dl |
1.1 |
} catch(Exception e){ |
391 |
|
|
unexpectedException(); |
392 |
|
|
} |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
dl |
1.17 |
* tryAcquireNanos on an exclusively held sync times out |
397 |
dl |
1.1 |
*/ |
398 |
dl |
1.16 |
public void testAcquireNanos_Timeout() { |
399 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
400 |
|
|
sync.acquire(1); |
401 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
402 |
|
|
public void run() { |
403 |
|
|
try { |
404 |
dl |
1.17 |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
405 |
dl |
1.1 |
} catch (Exception ex) { |
406 |
|
|
threadUnexpectedException(); |
407 |
|
|
} |
408 |
|
|
} |
409 |
|
|
}); |
410 |
|
|
try { |
411 |
|
|
t.start(); |
412 |
|
|
t.join(); |
413 |
dl |
1.17 |
sync.release(1); |
414 |
dl |
1.1 |
} catch(Exception e){ |
415 |
|
|
unexpectedException(); |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
|
|
419 |
|
|
|
420 |
|
|
/** |
421 |
dl |
1.13 |
* getState is true when acquired and false when not |
422 |
dl |
1.1 |
*/ |
423 |
dl |
1.13 |
public void testGetState() { |
424 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
425 |
|
|
sync.acquire(1); |
426 |
|
|
assertTrue(sync.isHeldExclusively()); |
427 |
|
|
sync.release(1); |
428 |
|
|
assertFalse(sync.isHeldExclusively()); |
429 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
430 |
|
|
public void run() { |
431 |
dl |
1.17 |
sync.acquire(1); |
432 |
dl |
1.1 |
try { |
433 |
|
|
Thread.sleep(SMALL_DELAY_MS); |
434 |
|
|
} |
435 |
|
|
catch(Exception e) { |
436 |
|
|
threadUnexpectedException(); |
437 |
|
|
} |
438 |
dl |
1.17 |
sync.release(1); |
439 |
dl |
1.1 |
} |
440 |
|
|
}); |
441 |
|
|
try { |
442 |
|
|
t.start(); |
443 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
444 |
dl |
1.17 |
assertTrue(sync.isHeldExclusively()); |
445 |
dl |
1.1 |
t.join(); |
446 |
dl |
1.17 |
assertFalse(sync.isHeldExclusively()); |
447 |
dl |
1.1 |
} catch(Exception e){ |
448 |
|
|
unexpectedException(); |
449 |
|
|
} |
450 |
|
|
} |
451 |
|
|
|
452 |
|
|
|
453 |
|
|
/** |
454 |
dl |
1.16 |
* acquireInterruptibly is interruptible. |
455 |
dl |
1.1 |
*/ |
456 |
dl |
1.12 |
public void testAcquireInterruptibly1() { |
457 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
458 |
|
|
sync.acquire(1); |
459 |
|
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
460 |
dl |
1.1 |
try { |
461 |
|
|
t.start(); |
462 |
|
|
t.interrupt(); |
463 |
dl |
1.17 |
sync.release(1); |
464 |
dl |
1.1 |
t.join(); |
465 |
|
|
} catch(Exception e){ |
466 |
|
|
unexpectedException(); |
467 |
|
|
} |
468 |
|
|
} |
469 |
|
|
|
470 |
|
|
/** |
471 |
dl |
1.16 |
* acquireInterruptibly succeeds when released, else is interruptible |
472 |
dl |
1.1 |
*/ |
473 |
dl |
1.12 |
public void testAcquireInterruptibly2() { |
474 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
475 |
dl |
1.1 |
try { |
476 |
dl |
1.17 |
sync.acquireInterruptibly(1); |
477 |
dl |
1.1 |
} catch(Exception e) { |
478 |
|
|
unexpectedException(); |
479 |
|
|
} |
480 |
dl |
1.17 |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
481 |
dl |
1.1 |
try { |
482 |
|
|
t.start(); |
483 |
|
|
t.interrupt(); |
484 |
dl |
1.17 |
assertTrue(sync.isHeldExclusively()); |
485 |
dl |
1.1 |
t.join(); |
486 |
|
|
} catch(Exception e){ |
487 |
|
|
unexpectedException(); |
488 |
|
|
} |
489 |
|
|
} |
490 |
|
|
|
491 |
|
|
/** |
492 |
dl |
1.17 |
* owns is true for a condition created by sync else false |
493 |
dl |
1.12 |
*/ |
494 |
|
|
public void testOwns() { |
495 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
496 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
497 |
|
|
final Mutex sync2 = new Mutex(); |
498 |
|
|
assertTrue(sync.owns(c)); |
499 |
|
|
assertFalse(sync2.owns(c)); |
500 |
dl |
1.12 |
} |
501 |
|
|
|
502 |
|
|
/** |
503 |
dl |
1.17 |
* Calling await without holding sync throws IllegalMonitorStateException |
504 |
dl |
1.1 |
*/ |
505 |
|
|
public void testAwait_IllegalMonitor() { |
506 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
507 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
dl |
1.1 |
try { |
509 |
|
|
c.await(); |
510 |
|
|
shouldThrow(); |
511 |
|
|
} |
512 |
|
|
catch (IllegalMonitorStateException success) { |
513 |
|
|
} |
514 |
|
|
catch (Exception ex) { |
515 |
|
|
unexpectedException(); |
516 |
|
|
} |
517 |
|
|
} |
518 |
|
|
|
519 |
|
|
/** |
520 |
dl |
1.17 |
* Calling signal without holding sync throws IllegalMonitorStateException |
521 |
dl |
1.1 |
*/ |
522 |
|
|
public void testSignal_IllegalMonitor() { |
523 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
524 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
525 |
dl |
1.1 |
try { |
526 |
|
|
c.signal(); |
527 |
|
|
shouldThrow(); |
528 |
|
|
} |
529 |
|
|
catch (IllegalMonitorStateException success) { |
530 |
|
|
} |
531 |
|
|
catch (Exception ex) { |
532 |
|
|
unexpectedException(); |
533 |
|
|
} |
534 |
|
|
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
|
|
* awaitNanos without a signal times out |
538 |
|
|
*/ |
539 |
|
|
public void testAwaitNanos_Timeout() { |
540 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
541 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
542 |
dl |
1.1 |
try { |
543 |
dl |
1.17 |
sync.acquire(1); |
544 |
dl |
1.1 |
long t = c.awaitNanos(100); |
545 |
|
|
assertTrue(t <= 0); |
546 |
dl |
1.17 |
sync.release(1); |
547 |
dl |
1.1 |
} |
548 |
|
|
catch (Exception ex) { |
549 |
|
|
unexpectedException(); |
550 |
|
|
} |
551 |
|
|
} |
552 |
|
|
|
553 |
|
|
/** |
554 |
dl |
1.17 |
* Timed await without a signal times out |
555 |
dl |
1.1 |
*/ |
556 |
|
|
public void testAwait_Timeout() { |
557 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
558 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
559 |
dl |
1.1 |
try { |
560 |
dl |
1.17 |
sync.acquire(1); |
561 |
dl |
1.1 |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
562 |
dl |
1.17 |
sync.release(1); |
563 |
dl |
1.1 |
} |
564 |
|
|
catch (Exception ex) { |
565 |
|
|
unexpectedException(); |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
/** |
570 |
|
|
* awaitUntil without a signal times out |
571 |
|
|
*/ |
572 |
|
|
public void testAwaitUntil_Timeout() { |
573 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
574 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
575 |
dl |
1.1 |
try { |
576 |
dl |
1.17 |
sync.acquire(1); |
577 |
dl |
1.1 |
java.util.Date d = new java.util.Date(); |
578 |
|
|
assertFalse(c.awaitUntil(new java.util.Date(d.getTime() + 10))); |
579 |
dl |
1.17 |
sync.release(1); |
580 |
dl |
1.1 |
} |
581 |
|
|
catch (Exception ex) { |
582 |
|
|
unexpectedException(); |
583 |
|
|
} |
584 |
|
|
} |
585 |
|
|
|
586 |
|
|
/** |
587 |
|
|
* await returns when signalled |
588 |
|
|
*/ |
589 |
|
|
public void testAwait() { |
590 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
591 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
592 |
dl |
1.15 |
Thread t = new Thread(new Runnable() { |
593 |
|
|
public void run() { |
594 |
|
|
try { |
595 |
dl |
1.17 |
sync.acquire(1); |
596 |
dl |
1.15 |
c.await(); |
597 |
dl |
1.17 |
sync.release(1); |
598 |
dl |
1.15 |
} |
599 |
|
|
catch(InterruptedException e) { |
600 |
|
|
threadUnexpectedException(); |
601 |
|
|
} |
602 |
|
|
} |
603 |
|
|
}); |
604 |
|
|
|
605 |
|
|
try { |
606 |
|
|
t.start(); |
607 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
608 |
dl |
1.17 |
sync.acquire(1); |
609 |
dl |
1.15 |
c.signal(); |
610 |
dl |
1.17 |
sync.release(1); |
611 |
dl |
1.15 |
t.join(SHORT_DELAY_MS); |
612 |
|
|
assertFalse(t.isAlive()); |
613 |
|
|
} |
614 |
|
|
catch (Exception ex) { |
615 |
|
|
unexpectedException(); |
616 |
|
|
} |
617 |
|
|
} |
618 |
|
|
|
619 |
|
|
|
620 |
|
|
|
621 |
|
|
/** |
622 |
|
|
* hasWaiters throws NPE if null |
623 |
|
|
*/ |
624 |
|
|
public void testHasWaitersNPE() { |
625 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
626 |
dl |
1.15 |
try { |
627 |
dl |
1.17 |
sync.hasWaiters(null); |
628 |
dl |
1.15 |
shouldThrow(); |
629 |
|
|
} catch (NullPointerException success) { |
630 |
|
|
} catch (Exception ex) { |
631 |
|
|
unexpectedException(); |
632 |
|
|
} |
633 |
|
|
} |
634 |
|
|
|
635 |
|
|
/** |
636 |
|
|
* getWaitQueueLength throws NPE if null |
637 |
|
|
*/ |
638 |
|
|
public void testGetWaitQueueLengthNPE() { |
639 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
640 |
dl |
1.15 |
try { |
641 |
dl |
1.17 |
sync.getWaitQueueLength(null); |
642 |
dl |
1.15 |
shouldThrow(); |
643 |
|
|
} catch (NullPointerException success) { |
644 |
|
|
} catch (Exception ex) { |
645 |
|
|
unexpectedException(); |
646 |
|
|
} |
647 |
|
|
} |
648 |
|
|
|
649 |
|
|
|
650 |
|
|
/** |
651 |
|
|
* getWaitingThreads throws NPE if null |
652 |
|
|
*/ |
653 |
|
|
public void testGetWaitingThreadsNPE() { |
654 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
655 |
dl |
1.15 |
try { |
656 |
dl |
1.17 |
sync.getWaitingThreads(null); |
657 |
dl |
1.15 |
shouldThrow(); |
658 |
|
|
} catch (NullPointerException success) { |
659 |
|
|
} catch (Exception ex) { |
660 |
|
|
unexpectedException(); |
661 |
|
|
} |
662 |
|
|
} |
663 |
|
|
|
664 |
|
|
|
665 |
|
|
/** |
666 |
|
|
* hasWaiters throws IAE if not owned |
667 |
|
|
*/ |
668 |
|
|
public void testHasWaitersIAE() { |
669 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
670 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
671 |
|
|
final Mutex sync2 = new Mutex(); |
672 |
dl |
1.15 |
try { |
673 |
dl |
1.17 |
sync2.hasWaiters(c); |
674 |
dl |
1.15 |
shouldThrow(); |
675 |
|
|
} catch (IllegalArgumentException success) { |
676 |
|
|
} catch (Exception ex) { |
677 |
|
|
unexpectedException(); |
678 |
|
|
} |
679 |
|
|
} |
680 |
|
|
|
681 |
|
|
/** |
682 |
dl |
1.17 |
* hasWaiters throws IMSE if not synced |
683 |
dl |
1.15 |
*/ |
684 |
|
|
public void testHasWaitersIMSE() { |
685 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
686 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
687 |
dl |
1.15 |
try { |
688 |
dl |
1.17 |
sync.hasWaiters(c); |
689 |
dl |
1.15 |
shouldThrow(); |
690 |
|
|
} catch (IllegalMonitorStateException success) { |
691 |
|
|
} catch (Exception ex) { |
692 |
|
|
unexpectedException(); |
693 |
|
|
} |
694 |
|
|
} |
695 |
|
|
|
696 |
|
|
|
697 |
|
|
/** |
698 |
|
|
* getWaitQueueLength throws IAE if not owned |
699 |
|
|
*/ |
700 |
|
|
public void testGetWaitQueueLengthIAE() { |
701 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
702 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
703 |
|
|
final Mutex sync2 = new Mutex(); |
704 |
dl |
1.15 |
try { |
705 |
dl |
1.17 |
sync2.getWaitQueueLength(c); |
706 |
dl |
1.15 |
shouldThrow(); |
707 |
|
|
} catch (IllegalArgumentException success) { |
708 |
|
|
} catch (Exception ex) { |
709 |
|
|
unexpectedException(); |
710 |
|
|
} |
711 |
|
|
} |
712 |
|
|
|
713 |
|
|
/** |
714 |
dl |
1.17 |
* getWaitQueueLength throws IMSE if not synced |
715 |
dl |
1.15 |
*/ |
716 |
|
|
public void testGetWaitQueueLengthIMSE() { |
717 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
718 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
719 |
dl |
1.15 |
try { |
720 |
dl |
1.17 |
sync.getWaitQueueLength(c); |
721 |
dl |
1.15 |
shouldThrow(); |
722 |
|
|
} catch (IllegalMonitorStateException success) { |
723 |
|
|
} catch (Exception ex) { |
724 |
|
|
unexpectedException(); |
725 |
|
|
} |
726 |
|
|
} |
727 |
|
|
|
728 |
|
|
|
729 |
|
|
/** |
730 |
|
|
* getWaitingThreads throws IAE if not owned |
731 |
|
|
*/ |
732 |
|
|
public void testGetWaitingThreadsIAE() { |
733 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
734 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
735 |
|
|
final Mutex sync2 = new Mutex(); |
736 |
dl |
1.15 |
try { |
737 |
dl |
1.17 |
sync2.getWaitingThreads(c); |
738 |
dl |
1.15 |
shouldThrow(); |
739 |
|
|
} catch (IllegalArgumentException success) { |
740 |
|
|
} catch (Exception ex) { |
741 |
|
|
unexpectedException(); |
742 |
|
|
} |
743 |
|
|
} |
744 |
|
|
|
745 |
|
|
/** |
746 |
dl |
1.17 |
* getWaitingThreads throws IMSE if not synced |
747 |
dl |
1.15 |
*/ |
748 |
|
|
public void testGetWaitingThreadsIMSE() { |
749 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
750 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
751 |
dl |
1.15 |
try { |
752 |
dl |
1.17 |
sync.getWaitingThreads(c); |
753 |
dl |
1.15 |
shouldThrow(); |
754 |
|
|
} catch (IllegalMonitorStateException success) { |
755 |
|
|
} catch (Exception ex) { |
756 |
|
|
unexpectedException(); |
757 |
|
|
} |
758 |
|
|
} |
759 |
|
|
|
760 |
|
|
|
761 |
|
|
|
762 |
|
|
/** |
763 |
|
|
* hasWaiters returns true when a thread is waiting, else false |
764 |
|
|
*/ |
765 |
|
|
public void testHasWaiters() { |
766 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
767 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
768 |
dl |
1.1 |
Thread t = new Thread(new Runnable() { |
769 |
|
|
public void run() { |
770 |
|
|
try { |
771 |
dl |
1.17 |
sync.acquire(1); |
772 |
|
|
threadAssertFalse(sync.hasWaiters(c)); |
773 |
|
|
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
774 |
dl |
1.15 |
c.await(); |
775 |
dl |
1.17 |
sync.release(1); |
776 |
dl |
1.15 |
} |
777 |
|
|
catch(InterruptedException e) { |
778 |
|
|
threadUnexpectedException(); |
779 |
|
|
} |
780 |
|
|
} |
781 |
|
|
}); |
782 |
|
|
|
783 |
|
|
try { |
784 |
|
|
t.start(); |
785 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
786 |
dl |
1.17 |
sync.acquire(1); |
787 |
|
|
assertTrue(sync.hasWaiters(c)); |
788 |
|
|
assertEquals(1, sync.getWaitQueueLength(c)); |
789 |
dl |
1.15 |
c.signal(); |
790 |
dl |
1.17 |
sync.release(1); |
791 |
dl |
1.15 |
Thread.sleep(SHORT_DELAY_MS); |
792 |
dl |
1.17 |
sync.acquire(1); |
793 |
|
|
assertFalse(sync.hasWaiters(c)); |
794 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
795 |
|
|
sync.release(1); |
796 |
dl |
1.15 |
t.join(SHORT_DELAY_MS); |
797 |
|
|
assertFalse(t.isAlive()); |
798 |
|
|
} |
799 |
|
|
catch (Exception ex) { |
800 |
|
|
unexpectedException(); |
801 |
|
|
} |
802 |
|
|
} |
803 |
|
|
|
804 |
|
|
/** |
805 |
|
|
* getWaitQueueLength returns number of waiting threads |
806 |
|
|
*/ |
807 |
|
|
public void testGetWaitQueueLength() { |
808 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
809 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
810 |
dl |
1.15 |
Thread t1 = new Thread(new Runnable() { |
811 |
|
|
public void run() { |
812 |
|
|
try { |
813 |
dl |
1.17 |
sync.acquire(1); |
814 |
|
|
threadAssertFalse(sync.hasWaiters(c)); |
815 |
|
|
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
816 |
dl |
1.15 |
c.await(); |
817 |
dl |
1.17 |
sync.release(1); |
818 |
dl |
1.15 |
} |
819 |
|
|
catch(InterruptedException e) { |
820 |
|
|
threadUnexpectedException(); |
821 |
|
|
} |
822 |
|
|
} |
823 |
|
|
}); |
824 |
|
|
|
825 |
|
|
Thread t2 = new Thread(new Runnable() { |
826 |
|
|
public void run() { |
827 |
|
|
try { |
828 |
dl |
1.17 |
sync.acquire(1); |
829 |
|
|
threadAssertTrue(sync.hasWaiters(c)); |
830 |
|
|
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
831 |
dl |
1.15 |
c.await(); |
832 |
dl |
1.17 |
sync.release(1); |
833 |
dl |
1.15 |
} |
834 |
|
|
catch(InterruptedException e) { |
835 |
|
|
threadUnexpectedException(); |
836 |
|
|
} |
837 |
|
|
} |
838 |
|
|
}); |
839 |
|
|
|
840 |
|
|
try { |
841 |
|
|
t1.start(); |
842 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
843 |
|
|
t2.start(); |
844 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
845 |
dl |
1.17 |
sync.acquire(1); |
846 |
|
|
assertTrue(sync.hasWaiters(c)); |
847 |
|
|
assertEquals(2, sync.getWaitQueueLength(c)); |
848 |
dl |
1.15 |
c.signalAll(); |
849 |
dl |
1.17 |
sync.release(1); |
850 |
dl |
1.15 |
Thread.sleep(SHORT_DELAY_MS); |
851 |
dl |
1.17 |
sync.acquire(1); |
852 |
|
|
assertFalse(sync.hasWaiters(c)); |
853 |
|
|
assertEquals(0, sync.getWaitQueueLength(c)); |
854 |
|
|
sync.release(1); |
855 |
dl |
1.15 |
t1.join(SHORT_DELAY_MS); |
856 |
|
|
t2.join(SHORT_DELAY_MS); |
857 |
|
|
assertFalse(t1.isAlive()); |
858 |
|
|
assertFalse(t2.isAlive()); |
859 |
|
|
} |
860 |
|
|
catch (Exception ex) { |
861 |
|
|
unexpectedException(); |
862 |
|
|
} |
863 |
|
|
} |
864 |
|
|
|
865 |
|
|
/** |
866 |
|
|
* getWaitingThreads returns only and all waiting threads |
867 |
|
|
*/ |
868 |
|
|
public void testGetWaitingThreads() { |
869 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
870 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
871 |
dl |
1.15 |
Thread t1 = new Thread(new Runnable() { |
872 |
|
|
public void run() { |
873 |
|
|
try { |
874 |
dl |
1.17 |
sync.acquire(1); |
875 |
|
|
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
876 |
dl |
1.15 |
c.await(); |
877 |
dl |
1.17 |
sync.release(1); |
878 |
dl |
1.15 |
} |
879 |
|
|
catch(InterruptedException e) { |
880 |
|
|
threadUnexpectedException(); |
881 |
|
|
} |
882 |
|
|
} |
883 |
|
|
}); |
884 |
|
|
|
885 |
|
|
Thread t2 = new Thread(new Runnable() { |
886 |
|
|
public void run() { |
887 |
|
|
try { |
888 |
dl |
1.17 |
sync.acquire(1); |
889 |
|
|
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
890 |
dl |
1.1 |
c.await(); |
891 |
dl |
1.17 |
sync.release(1); |
892 |
dl |
1.1 |
} |
893 |
|
|
catch(InterruptedException e) { |
894 |
|
|
threadUnexpectedException(); |
895 |
|
|
} |
896 |
|
|
} |
897 |
|
|
}); |
898 |
|
|
|
899 |
|
|
try { |
900 |
dl |
1.17 |
sync.acquire(1); |
901 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
902 |
|
|
sync.release(1); |
903 |
dl |
1.15 |
t1.start(); |
904 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
905 |
|
|
t2.start(); |
906 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
907 |
dl |
1.17 |
sync.acquire(1); |
908 |
|
|
assertTrue(sync.hasWaiters(c)); |
909 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t1)); |
910 |
|
|
assertTrue(sync.getWaitingThreads(c).contains(t2)); |
911 |
dl |
1.15 |
c.signalAll(); |
912 |
dl |
1.17 |
sync.release(1); |
913 |
dl |
1.15 |
Thread.sleep(SHORT_DELAY_MS); |
914 |
dl |
1.17 |
sync.acquire(1); |
915 |
|
|
assertFalse(sync.hasWaiters(c)); |
916 |
|
|
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
917 |
|
|
sync.release(1); |
918 |
dl |
1.15 |
t1.join(SHORT_DELAY_MS); |
919 |
|
|
t2.join(SHORT_DELAY_MS); |
920 |
|
|
assertFalse(t1.isAlive()); |
921 |
|
|
assertFalse(t2.isAlive()); |
922 |
|
|
} |
923 |
|
|
catch (Exception ex) { |
924 |
|
|
unexpectedException(); |
925 |
|
|
} |
926 |
|
|
} |
927 |
|
|
|
928 |
|
|
|
929 |
|
|
|
930 |
|
|
/** |
931 |
|
|
* awaitUninterruptibly doesn't abort on interrupt |
932 |
|
|
*/ |
933 |
|
|
public void testAwaitUninterruptibly() { |
934 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
935 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
936 |
dl |
1.15 |
Thread t = new Thread(new Runnable() { |
937 |
|
|
public void run() { |
938 |
dl |
1.17 |
sync.acquire(1); |
939 |
dl |
1.15 |
c.awaitUninterruptibly(); |
940 |
dl |
1.17 |
sync.release(1); |
941 |
dl |
1.15 |
} |
942 |
|
|
}); |
943 |
|
|
|
944 |
|
|
try { |
945 |
dl |
1.1 |
t.start(); |
946 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
947 |
dl |
1.15 |
t.interrupt(); |
948 |
dl |
1.17 |
sync.acquire(1); |
949 |
dl |
1.1 |
c.signal(); |
950 |
dl |
1.17 |
sync.release(1); |
951 |
dl |
1.15 |
assert(t.isInterrupted()); |
952 |
|
|
t.join(SHORT_DELAY_MS); |
953 |
|
|
assertFalse(t.isAlive()); |
954 |
|
|
} |
955 |
|
|
catch (Exception ex) { |
956 |
|
|
unexpectedException(); |
957 |
|
|
} |
958 |
|
|
} |
959 |
|
|
|
960 |
|
|
/** |
961 |
|
|
* await is interruptible |
962 |
|
|
*/ |
963 |
|
|
public void testAwait_Interrupt() { |
964 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
965 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
966 |
dl |
1.15 |
Thread t = new Thread(new Runnable() { |
967 |
|
|
public void run() { |
968 |
|
|
try { |
969 |
dl |
1.17 |
sync.acquire(1); |
970 |
dl |
1.15 |
c.await(); |
971 |
dl |
1.17 |
sync.release(1); |
972 |
dl |
1.15 |
threadShouldThrow(); |
973 |
|
|
} |
974 |
|
|
catch(InterruptedException success) { |
975 |
|
|
} |
976 |
|
|
} |
977 |
|
|
}); |
978 |
|
|
|
979 |
|
|
try { |
980 |
|
|
t.start(); |
981 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
982 |
|
|
t.interrupt(); |
983 |
|
|
t.join(SHORT_DELAY_MS); |
984 |
|
|
assertFalse(t.isAlive()); |
985 |
|
|
} |
986 |
|
|
catch (Exception ex) { |
987 |
|
|
unexpectedException(); |
988 |
|
|
} |
989 |
|
|
} |
990 |
|
|
|
991 |
|
|
/** |
992 |
|
|
* awaitNanos is interruptible |
993 |
|
|
*/ |
994 |
|
|
public void testAwaitNanos_Interrupt() { |
995 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
996 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
997 |
dl |
1.15 |
Thread t = new Thread(new Runnable() { |
998 |
|
|
public void run() { |
999 |
|
|
try { |
1000 |
dl |
1.17 |
sync.acquire(1); |
1001 |
dl |
1.15 |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
1002 |
dl |
1.17 |
sync.release(1); |
1003 |
dl |
1.15 |
threadShouldThrow(); |
1004 |
|
|
} |
1005 |
|
|
catch(InterruptedException success) { |
1006 |
|
|
} |
1007 |
|
|
} |
1008 |
|
|
}); |
1009 |
|
|
|
1010 |
|
|
try { |
1011 |
|
|
t.start(); |
1012 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1013 |
|
|
t.interrupt(); |
1014 |
|
|
t.join(SHORT_DELAY_MS); |
1015 |
|
|
assertFalse(t.isAlive()); |
1016 |
|
|
} |
1017 |
|
|
catch (Exception ex) { |
1018 |
|
|
unexpectedException(); |
1019 |
|
|
} |
1020 |
|
|
} |
1021 |
|
|
|
1022 |
|
|
/** |
1023 |
|
|
* awaitUntil is interruptible |
1024 |
|
|
*/ |
1025 |
|
|
public void testAwaitUntil_Interrupt() { |
1026 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
1027 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1028 |
dl |
1.15 |
Thread t = new Thread(new Runnable() { |
1029 |
|
|
public void run() { |
1030 |
|
|
try { |
1031 |
dl |
1.17 |
sync.acquire(1); |
1032 |
dl |
1.15 |
java.util.Date d = new java.util.Date(); |
1033 |
|
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
1034 |
dl |
1.17 |
sync.release(1); |
1035 |
dl |
1.15 |
threadShouldThrow(); |
1036 |
|
|
} |
1037 |
|
|
catch(InterruptedException success) { |
1038 |
|
|
} |
1039 |
|
|
} |
1040 |
|
|
}); |
1041 |
|
|
|
1042 |
|
|
try { |
1043 |
|
|
t.start(); |
1044 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1045 |
|
|
t.interrupt(); |
1046 |
dl |
1.1 |
t.join(SHORT_DELAY_MS); |
1047 |
|
|
assertFalse(t.isAlive()); |
1048 |
|
|
} |
1049 |
|
|
catch (Exception ex) { |
1050 |
dl |
1.12 |
unexpectedException(); |
1051 |
|
|
} |
1052 |
|
|
} |
1053 |
|
|
|
1054 |
dl |
1.14 |
/** |
1055 |
dl |
1.15 |
* signalAll wakes up all threads |
1056 |
|
|
*/ |
1057 |
|
|
public void testSignalAll() { |
1058 |
dl |
1.17 |
final Mutex sync = new Mutex(); |
1059 |
|
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1060 |
dl |
1.15 |
Thread t1 = new Thread(new Runnable() { |
1061 |
|
|
public void run() { |
1062 |
|
|
try { |
1063 |
dl |
1.17 |
sync.acquire(1); |
1064 |
dl |
1.15 |
c.await(); |
1065 |
dl |
1.17 |
sync.release(1); |
1066 |
dl |
1.15 |
} |
1067 |
|
|
catch(InterruptedException e) { |
1068 |
|
|
threadUnexpectedException(); |
1069 |
|
|
} |
1070 |
|
|
} |
1071 |
|
|
}); |
1072 |
|
|
|
1073 |
|
|
Thread t2 = new Thread(new Runnable() { |
1074 |
|
|
public void run() { |
1075 |
|
|
try { |
1076 |
dl |
1.17 |
sync.acquire(1); |
1077 |
dl |
1.15 |
c.await(); |
1078 |
dl |
1.17 |
sync.release(1); |
1079 |
dl |
1.15 |
} |
1080 |
|
|
catch(InterruptedException e) { |
1081 |
|
|
threadUnexpectedException(); |
1082 |
|
|
} |
1083 |
|
|
} |
1084 |
|
|
}); |
1085 |
|
|
|
1086 |
|
|
try { |
1087 |
|
|
t1.start(); |
1088 |
|
|
t2.start(); |
1089 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1090 |
dl |
1.17 |
sync.acquire(1); |
1091 |
dl |
1.15 |
c.signalAll(); |
1092 |
dl |
1.17 |
sync.release(1); |
1093 |
dl |
1.15 |
t1.join(SHORT_DELAY_MS); |
1094 |
|
|
t2.join(SHORT_DELAY_MS); |
1095 |
|
|
assertFalse(t1.isAlive()); |
1096 |
|
|
assertFalse(t2.isAlive()); |
1097 |
|
|
} |
1098 |
|
|
catch (Exception ex) { |
1099 |
|
|
unexpectedException(); |
1100 |
|
|
} |
1101 |
|
|
} |
1102 |
|
|
|
1103 |
|
|
|
1104 |
|
|
/** |
1105 |
dl |
1.14 |
* toString indicates current state |
1106 |
|
|
*/ |
1107 |
|
|
public void testToString() { |
1108 |
dl |
1.17 |
Mutex sync = new Mutex(); |
1109 |
|
|
String us = sync.toString(); |
1110 |
dl |
1.14 |
assertTrue(us.indexOf("State = 0") >= 0); |
1111 |
dl |
1.17 |
sync.acquire(1); |
1112 |
|
|
String ls = sync.toString(); |
1113 |
dl |
1.14 |
assertTrue(ls.indexOf("State = 1") >= 0); |
1114 |
|
|
} |
1115 |
|
|
|
1116 |
|
|
/** |
1117 |
|
|
* A serialized AQS deserializes with current state |
1118 |
|
|
*/ |
1119 |
|
|
public void testSerialization() { |
1120 |
|
|
Mutex l = new Mutex(); |
1121 |
dl |
1.16 |
l.acquire(1); |
1122 |
|
|
assertTrue(l.isHeldExclusively()); |
1123 |
dl |
1.14 |
|
1124 |
|
|
try { |
1125 |
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
1126 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
1127 |
|
|
out.writeObject(l); |
1128 |
|
|
out.close(); |
1129 |
|
|
|
1130 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
1131 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1132 |
|
|
Mutex r = (Mutex) in.readObject(); |
1133 |
dl |
1.16 |
assertTrue(r.isHeldExclusively()); |
1134 |
dl |
1.14 |
} catch(Exception e){ |
1135 |
|
|
e.printStackTrace(); |
1136 |
|
|
unexpectedException(); |
1137 |
|
|
} |
1138 |
|
|
} |
1139 |
|
|
|
1140 |
dl |
1.12 |
|
1141 |
|
|
/** |
1142 |
dl |
1.15 |
* tryReleaseShared setting state changes getState |
1143 |
dl |
1.12 |
*/ |
1144 |
dl |
1.15 |
public void testGetStateWithReleaseShared() { |
1145 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1146 |
|
|
assertFalse(l.isSignalled()); |
1147 |
|
|
l.releaseShared(0); |
1148 |
|
|
assertTrue(l.isSignalled()); |
1149 |
|
|
} |
1150 |
|
|
|
1151 |
|
|
/** |
1152 |
dl |
1.17 |
* releaseShared has no effect when already signalled |
1153 |
dl |
1.12 |
*/ |
1154 |
dl |
1.13 |
public void testReleaseShared() { |
1155 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1156 |
|
|
assertFalse(l.isSignalled()); |
1157 |
|
|
l.releaseShared(0); |
1158 |
|
|
assertTrue(l.isSignalled()); |
1159 |
|
|
l.releaseShared(0); |
1160 |
|
|
assertTrue(l.isSignalled()); |
1161 |
|
|
} |
1162 |
|
|
|
1163 |
|
|
/** |
1164 |
|
|
* acquireSharedInterruptibly returns after release, but not before |
1165 |
|
|
*/ |
1166 |
dl |
1.13 |
public void testAcquireSharedInterruptibly() { |
1167 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1168 |
|
|
|
1169 |
|
|
Thread t = new Thread(new Runnable() { |
1170 |
|
|
public void run() { |
1171 |
|
|
try { |
1172 |
|
|
threadAssertFalse(l.isSignalled()); |
1173 |
|
|
l.acquireSharedInterruptibly(0); |
1174 |
|
|
threadAssertTrue(l.isSignalled()); |
1175 |
|
|
} catch(InterruptedException e){ |
1176 |
|
|
threadUnexpectedException(); |
1177 |
|
|
} |
1178 |
|
|
} |
1179 |
|
|
}); |
1180 |
|
|
try { |
1181 |
|
|
t.start(); |
1182 |
|
|
assertFalse(l.isSignalled()); |
1183 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1184 |
|
|
l.releaseShared(0); |
1185 |
|
|
assertTrue(l.isSignalled()); |
1186 |
|
|
t.join(); |
1187 |
|
|
} catch (InterruptedException e){ |
1188 |
|
|
unexpectedException(); |
1189 |
|
|
} |
1190 |
|
|
} |
1191 |
|
|
|
1192 |
|
|
|
1193 |
|
|
/** |
1194 |
|
|
* acquireSharedTimed returns after release |
1195 |
|
|
*/ |
1196 |
dl |
1.13 |
public void testAsquireSharedTimed() { |
1197 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1198 |
|
|
|
1199 |
|
|
Thread t = new Thread(new Runnable() { |
1200 |
|
|
public void run() { |
1201 |
|
|
try { |
1202 |
|
|
threadAssertFalse(l.isSignalled()); |
1203 |
dl |
1.16 |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1204 |
dl |
1.12 |
threadAssertTrue(l.isSignalled()); |
1205 |
|
|
|
1206 |
|
|
} catch(InterruptedException e){ |
1207 |
|
|
threadUnexpectedException(); |
1208 |
|
|
} |
1209 |
|
|
} |
1210 |
|
|
}); |
1211 |
|
|
try { |
1212 |
|
|
t.start(); |
1213 |
|
|
assertFalse(l.isSignalled()); |
1214 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1215 |
|
|
l.releaseShared(0); |
1216 |
|
|
assertTrue(l.isSignalled()); |
1217 |
|
|
t.join(); |
1218 |
|
|
} catch (InterruptedException e){ |
1219 |
|
|
unexpectedException(); |
1220 |
|
|
} |
1221 |
|
|
} |
1222 |
|
|
|
1223 |
|
|
/** |
1224 |
|
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1225 |
|
|
*/ |
1226 |
dl |
1.13 |
public void testAcquireSharedInterruptibly_InterruptedException() { |
1227 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1228 |
|
|
Thread t = new Thread(new Runnable() { |
1229 |
|
|
public void run() { |
1230 |
|
|
try { |
1231 |
|
|
threadAssertFalse(l.isSignalled()); |
1232 |
|
|
l.acquireSharedInterruptibly(0); |
1233 |
|
|
threadShouldThrow(); |
1234 |
|
|
} catch(InterruptedException success){} |
1235 |
|
|
} |
1236 |
|
|
}); |
1237 |
|
|
t.start(); |
1238 |
|
|
try { |
1239 |
|
|
assertFalse(l.isSignalled()); |
1240 |
|
|
t.interrupt(); |
1241 |
|
|
t.join(); |
1242 |
|
|
} catch (InterruptedException e){ |
1243 |
|
|
unexpectedException(); |
1244 |
|
|
} |
1245 |
|
|
} |
1246 |
|
|
|
1247 |
|
|
/** |
1248 |
|
|
* acquireSharedTimed throws IE if interrupted before released |
1249 |
|
|
*/ |
1250 |
dl |
1.13 |
public void testAcquireSharedNanos_InterruptedException() { |
1251 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1252 |
|
|
Thread t = new Thread(new Runnable() { |
1253 |
|
|
public void run() { |
1254 |
|
|
try { |
1255 |
|
|
threadAssertFalse(l.isSignalled()); |
1256 |
dl |
1.16 |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1257 |
dl |
1.12 |
threadShouldThrow(); |
1258 |
|
|
} catch(InterruptedException success){} |
1259 |
|
|
} |
1260 |
|
|
}); |
1261 |
|
|
t.start(); |
1262 |
|
|
try { |
1263 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1264 |
|
|
assertFalse(l.isSignalled()); |
1265 |
|
|
t.interrupt(); |
1266 |
|
|
t.join(); |
1267 |
|
|
} catch (InterruptedException e){ |
1268 |
|
|
unexpectedException(); |
1269 |
|
|
} |
1270 |
|
|
} |
1271 |
|
|
|
1272 |
|
|
/** |
1273 |
|
|
* acquireSharedTimed times out if not released before timeout |
1274 |
|
|
*/ |
1275 |
dl |
1.13 |
public void testAcquireSharedNanos_Timeout() { |
1276 |
dl |
1.12 |
final BooleanLatch l = new BooleanLatch(); |
1277 |
|
|
Thread t = new Thread(new Runnable() { |
1278 |
|
|
public void run() { |
1279 |
|
|
try { |
1280 |
|
|
threadAssertFalse(l.isSignalled()); |
1281 |
dl |
1.16 |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1282 |
dl |
1.12 |
} catch(InterruptedException ie){ |
1283 |
|
|
threadUnexpectedException(); |
1284 |
|
|
} |
1285 |
|
|
} |
1286 |
|
|
}); |
1287 |
|
|
t.start(); |
1288 |
|
|
try { |
1289 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1290 |
|
|
assertFalse(l.isSignalled()); |
1291 |
|
|
t.join(); |
1292 |
|
|
} catch (InterruptedException e){ |
1293 |
dl |
1.1 |
unexpectedException(); |
1294 |
|
|
} |
1295 |
|
|
} |
1296 |
dl |
1.14 |
|
1297 |
dl |
1.1 |
|
1298 |
|
|
} |