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. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
32 |
|
static class Mutex extends AbstractQueuedLongSynchronizer { |
33 |
|
// Use value > 32 bits for locked state |
34 |
|
static final long LOCKED = 1 << 48; |
35 |
< |
public boolean isHeldExclusively() { |
36 |
< |
return getState() == LOCKED; |
35 |
> |
public boolean isHeldExclusively() { |
36 |
> |
return getState() == LOCKED; |
37 |
|
} |
38 |
< |
|
38 |
> |
|
39 |
|
public boolean tryAcquire(long acquires) { |
40 |
|
return compareAndSetState(0, LOCKED); |
41 |
|
} |
42 |
< |
|
42 |
> |
|
43 |
|
public boolean tryRelease(long releases) { |
44 |
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
45 |
|
setState(0); |
46 |
|
return true; |
47 |
|
} |
48 |
< |
|
48 |
> |
|
49 |
|
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
50 |
|
|
51 |
|
} |
52 |
|
|
53 |
< |
|
53 |
> |
|
54 |
|
/** |
55 |
|
* A simple latch class, to test shared mode. |
56 |
|
*/ |
57 |
< |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
57 |
> |
static class BooleanLatch extends AbstractQueuedLongSynchronizer { |
58 |
|
public boolean isSignalled() { return getState() != 0; } |
59 |
|
|
60 |
|
public long tryAcquireShared(long ignore) { |
61 |
|
return isSignalled()? 1 : -1; |
62 |
|
} |
63 |
< |
|
63 |
> |
|
64 |
|
public boolean tryReleaseShared(long ignore) { |
65 |
|
setState(1 << 62); |
66 |
|
return true; |
76 |
|
public void run() { |
77 |
|
try { |
78 |
|
sync.acquireInterruptibly(1); |
79 |
< |
} catch(InterruptedException success){} |
79 |
> |
} catch (InterruptedException success) {} |
80 |
|
} |
81 |
|
} |
82 |
|
|
92 |
|
try { |
93 |
|
sync.acquireInterruptibly(1); |
94 |
|
threadShouldThrow(); |
95 |
< |
} catch(InterruptedException success){} |
95 |
> |
} catch (InterruptedException success) {} |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
|
/** |
100 |
|
* isHeldExclusively is false upon construction |
101 |
|
*/ |
102 |
< |
public void testIsHeldExclusively() { |
102 |
> |
public void testIsHeldExclusively() { |
103 |
|
Mutex rl = new Mutex(); |
104 |
|
assertFalse(rl.isHeldExclusively()); |
105 |
|
} |
106 |
< |
|
106 |
> |
|
107 |
|
/** |
108 |
|
* acquiring released sync succeeds |
109 |
|
*/ |
110 |
< |
public void testAcquire() { |
110 |
> |
public void testAcquire() { |
111 |
|
Mutex rl = new Mutex(); |
112 |
|
rl.acquire(1); |
113 |
|
assertTrue(rl.isHeldExclusively()); |
118 |
|
/** |
119 |
|
* tryAcquire on an released sync succeeds |
120 |
|
*/ |
121 |
< |
public void testTryAcquire() { |
121 |
> |
public void testTryAcquire() { |
122 |
|
Mutex rl = new Mutex(); |
123 |
|
assertTrue(rl.tryAcquire(1)); |
124 |
|
assertTrue(rl.isHeldExclusively()); |
128 |
|
/** |
129 |
|
* hasQueuedThreads reports whether there are waiting threads |
130 |
|
*/ |
131 |
< |
public void testhasQueuedThreads() { |
131 |
> |
public void testhasQueuedThreads() { |
132 |
|
final Mutex sync = new Mutex(); |
133 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
134 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
149 |
|
assertFalse(sync.hasQueuedThreads()); |
150 |
|
t1.join(); |
151 |
|
t2.join(); |
152 |
< |
} catch(Exception e){ |
152 |
> |
} catch (Exception e) { |
153 |
|
unexpectedException(); |
154 |
|
} |
155 |
< |
} |
155 |
> |
} |
156 |
|
|
157 |
|
/** |
158 |
|
* isQueued(null) throws NPE |
159 |
|
*/ |
160 |
< |
public void testIsQueuedNPE() { |
160 |
> |
public void testIsQueuedNPE() { |
161 |
|
final Mutex sync = new Mutex(); |
162 |
|
try { |
163 |
|
sync.isQueued(null); |
169 |
|
/** |
170 |
|
* isQueued reports whether a thread is queued. |
171 |
|
*/ |
172 |
< |
public void testIsQueued() { |
172 |
> |
public void testIsQueued() { |
173 |
|
final Mutex sync = new Mutex(); |
174 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
175 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
195 |
|
assertFalse(sync.isQueued(t2)); |
196 |
|
t1.join(); |
197 |
|
t2.join(); |
198 |
< |
} catch(Exception e){ |
198 |
> |
} catch (Exception e) { |
199 |
|
unexpectedException(); |
200 |
|
} |
201 |
< |
} |
201 |
> |
} |
202 |
|
|
203 |
|
/** |
204 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
205 |
|
*/ |
206 |
< |
public void testGetFirstQueuedThread() { |
206 |
> |
public void testGetFirstQueuedThread() { |
207 |
|
final Mutex sync = new Mutex(); |
208 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
209 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
225 |
|
assertNull(sync.getFirstQueuedThread()); |
226 |
|
t1.join(); |
227 |
|
t2.join(); |
228 |
< |
} catch(Exception e){ |
228 |
> |
} catch (Exception e) { |
229 |
|
unexpectedException(); |
230 |
|
} |
231 |
< |
} |
231 |
> |
} |
232 |
|
|
233 |
|
|
234 |
|
/** |
235 |
|
* hasContended reports false if no thread has ever blocked, else true |
236 |
|
*/ |
237 |
< |
public void testHasContended() { |
237 |
> |
public void testHasContended() { |
238 |
|
final Mutex sync = new Mutex(); |
239 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
240 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
255 |
|
assertTrue(sync.hasContended()); |
256 |
|
t1.join(); |
257 |
|
t2.join(); |
258 |
< |
} catch(Exception e){ |
258 |
> |
} catch (Exception e) { |
259 |
|
unexpectedException(); |
260 |
|
} |
261 |
< |
} |
261 |
> |
} |
262 |
|
|
263 |
|
/** |
264 |
|
* getQueuedThreads includes waiting threads |
265 |
|
*/ |
266 |
< |
public void testGetQueuedThreads() { |
266 |
> |
public void testGetQueuedThreads() { |
267 |
|
final Mutex sync = new Mutex(); |
268 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
269 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
287 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
288 |
|
t1.join(); |
289 |
|
t2.join(); |
290 |
< |
} catch(Exception e){ |
290 |
> |
} catch (Exception e) { |
291 |
|
unexpectedException(); |
292 |
|
} |
293 |
< |
} |
293 |
> |
} |
294 |
|
|
295 |
|
/** |
296 |
|
* getExclusiveQueuedThreads includes waiting threads |
297 |
|
*/ |
298 |
< |
public void testGetExclusiveQueuedThreads() { |
298 |
> |
public void testGetExclusiveQueuedThreads() { |
299 |
|
final Mutex sync = new Mutex(); |
300 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
301 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
319 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
320 |
|
t1.join(); |
321 |
|
t2.join(); |
322 |
< |
} catch(Exception e){ |
322 |
> |
} catch (Exception e) { |
323 |
|
unexpectedException(); |
324 |
|
} |
325 |
< |
} |
325 |
> |
} |
326 |
|
|
327 |
|
/** |
328 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
329 |
|
*/ |
330 |
< |
public void testGetSharedQueuedThreads() { |
330 |
> |
public void testGetSharedQueuedThreads() { |
331 |
|
final Mutex sync = new Mutex(); |
332 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
333 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
349 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
350 |
|
t1.join(); |
351 |
|
t2.join(); |
352 |
< |
} catch(Exception e){ |
352 |
> |
} catch (Exception e) { |
353 |
|
unexpectedException(); |
354 |
|
} |
355 |
< |
} |
355 |
> |
} |
356 |
|
|
357 |
|
/** |
358 |
|
* tryAcquireNanos is interruptible. |
359 |
|
*/ |
360 |
< |
public void testInterruptedException2() { |
360 |
> |
public void testInterruptedException2() { |
361 |
|
final Mutex sync = new Mutex(); |
362 |
|
sync.acquire(1); |
363 |
|
Thread t = new Thread(new Runnable() { |
365 |
|
try { |
366 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
367 |
|
threadShouldThrow(); |
368 |
< |
} catch(InterruptedException success){} |
368 |
> |
} catch (InterruptedException success) {} |
369 |
|
} |
370 |
|
}); |
371 |
|
try { |
372 |
|
t.start(); |
373 |
|
t.interrupt(); |
374 |
< |
} catch(Exception e){ |
374 |
> |
} catch (Exception e) { |
375 |
|
unexpectedException(); |
376 |
|
} |
377 |
|
} |
380 |
|
/** |
381 |
|
* TryAcquire on exclusively held sync fails |
382 |
|
*/ |
383 |
< |
public void testTryAcquireWhenSynced() { |
383 |
> |
public void testTryAcquireWhenSynced() { |
384 |
|
final Mutex sync = new Mutex(); |
385 |
|
sync.acquire(1); |
386 |
|
Thread t = new Thread(new Runnable() { |
392 |
|
t.start(); |
393 |
|
t.join(); |
394 |
|
sync.release(1); |
395 |
< |
} catch(Exception e){ |
395 |
> |
} catch (Exception e) { |
396 |
|
unexpectedException(); |
397 |
|
} |
398 |
< |
} |
398 |
> |
} |
399 |
|
|
400 |
|
/** |
401 |
|
* tryAcquireNanos on an exclusively held sync times out |
402 |
|
*/ |
403 |
< |
public void testAcquireNanos_Timeout() { |
403 |
> |
public void testAcquireNanos_Timeout() { |
404 |
|
final Mutex sync = new Mutex(); |
405 |
|
sync.acquire(1); |
406 |
|
Thread t = new Thread(new Runnable() { |
416 |
|
t.start(); |
417 |
|
t.join(); |
418 |
|
sync.release(1); |
419 |
< |
} catch(Exception e){ |
419 |
> |
} catch (Exception e) { |
420 |
|
unexpectedException(); |
421 |
|
} |
422 |
< |
} |
423 |
< |
|
424 |
< |
|
422 |
> |
} |
423 |
> |
|
424 |
> |
|
425 |
|
/** |
426 |
|
* getState is true when acquired and false when not |
427 |
|
*/ |
431 |
|
assertTrue(sync.isHeldExclusively()); |
432 |
|
sync.release(1); |
433 |
|
assertFalse(sync.isHeldExclusively()); |
434 |
< |
Thread t = new Thread(new Runnable() { |
434 |
> |
Thread t = new Thread(new Runnable() { |
435 |
|
public void run() { |
436 |
|
sync.acquire(1); |
437 |
|
try { |
438 |
|
Thread.sleep(SMALL_DELAY_MS); |
439 |
|
} |
440 |
< |
catch(Exception e) { |
440 |
> |
catch (Exception e) { |
441 |
|
threadUnexpectedException(); |
442 |
|
} |
443 |
|
sync.release(1); |
449 |
|
assertTrue(sync.isHeldExclusively()); |
450 |
|
t.join(); |
451 |
|
assertFalse(sync.isHeldExclusively()); |
452 |
< |
} catch(Exception e){ |
452 |
> |
} catch (Exception e) { |
453 |
|
unexpectedException(); |
454 |
|
} |
455 |
|
} |
458 |
|
/** |
459 |
|
* acquireInterruptibly is interruptible. |
460 |
|
*/ |
461 |
< |
public void testAcquireInterruptibly1() { |
461 |
> |
public void testAcquireInterruptibly1() { |
462 |
|
final Mutex sync = new Mutex(); |
463 |
|
sync.acquire(1); |
464 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
466 |
|
t.start(); |
467 |
|
Thread.sleep(SHORT_DELAY_MS); |
468 |
|
t.interrupt(); |
469 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
470 |
|
sync.release(1); |
471 |
|
t.join(); |
472 |
< |
} catch(Exception e){ |
472 |
> |
} catch (Exception e) { |
473 |
|
unexpectedException(); |
474 |
|
} |
475 |
< |
} |
475 |
> |
} |
476 |
|
|
477 |
|
/** |
478 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
479 |
|
*/ |
480 |
|
public void testAcquireInterruptibly2() { |
481 |
< |
final Mutex sync = new Mutex(); |
481 |
> |
final Mutex sync = new Mutex(); |
482 |
|
try { |
483 |
|
sync.acquireInterruptibly(1); |
484 |
< |
} catch(Exception e) { |
484 |
> |
} catch (Exception e) { |
485 |
|
unexpectedException(); |
486 |
|
} |
487 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
490 |
|
t.interrupt(); |
491 |
|
assertTrue(sync.isHeldExclusively()); |
492 |
|
t.join(); |
493 |
< |
} catch(Exception e){ |
493 |
> |
} catch (Exception e) { |
494 |
|
unexpectedException(); |
495 |
|
} |
496 |
|
} |
499 |
|
* owns is true for a condition created by sync else false |
500 |
|
*/ |
501 |
|
public void testOwns() { |
502 |
< |
final Mutex sync = new Mutex(); |
502 |
> |
final Mutex sync = new Mutex(); |
503 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
504 |
|
final Mutex sync2 = new Mutex(); |
505 |
|
assertTrue(sync.owns(c)); |
510 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
511 |
|
*/ |
512 |
|
public void testAwait_IllegalMonitor() { |
513 |
< |
final Mutex sync = new Mutex(); |
513 |
> |
final Mutex sync = new Mutex(); |
514 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
515 |
|
try { |
516 |
|
c.await(); |
527 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
528 |
|
*/ |
529 |
|
public void testSignal_IllegalMonitor() { |
530 |
< |
final Mutex sync = new Mutex(); |
530 |
> |
final Mutex sync = new Mutex(); |
531 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
532 |
|
try { |
533 |
|
c.signal(); |
544 |
|
* awaitNanos without a signal times out |
545 |
|
*/ |
546 |
|
public void testAwaitNanos_Timeout() { |
547 |
< |
final Mutex sync = new Mutex(); |
547 |
> |
final Mutex sync = new Mutex(); |
548 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
549 |
|
try { |
550 |
|
sync.acquire(1); |
561 |
|
* Timed await without a signal times out |
562 |
|
*/ |
563 |
|
public void testAwait_Timeout() { |
564 |
< |
final Mutex sync = new Mutex(); |
564 |
> |
final Mutex sync = new Mutex(); |
565 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
566 |
|
try { |
567 |
|
sync.acquire(1); |
577 |
|
* awaitUntil without a signal times out |
578 |
|
*/ |
579 |
|
public void testAwaitUntil_Timeout() { |
580 |
< |
final Mutex sync = new Mutex(); |
580 |
> |
final Mutex sync = new Mutex(); |
581 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
582 |
|
try { |
583 |
|
sync.acquire(1); |
594 |
|
* await returns when signalled |
595 |
|
*/ |
596 |
|
public void testAwait() { |
597 |
< |
final Mutex sync = new Mutex(); |
597 |
> |
final Mutex sync = new Mutex(); |
598 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
599 |
< |
Thread t = new Thread(new Runnable() { |
599 |
> |
Thread t = new Thread(new Runnable() { |
600 |
|
public void run() { |
601 |
|
try { |
602 |
|
sync.acquire(1); |
603 |
|
c.await(); |
604 |
|
sync.release(1); |
605 |
|
} |
606 |
< |
catch(InterruptedException e) { |
606 |
> |
catch (InterruptedException e) { |
607 |
|
threadUnexpectedException(); |
608 |
|
} |
609 |
|
} |
737 |
|
* getWaitingThreads throws IAE if not owned |
738 |
|
*/ |
739 |
|
public void testGetWaitingThreadsIAE() { |
740 |
< |
final Mutex sync = new Mutex(); |
740 |
> |
final Mutex sync = new Mutex(); |
741 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
742 |
< |
final Mutex sync2 = new Mutex(); |
742 |
> |
final Mutex sync2 = new Mutex(); |
743 |
|
try { |
744 |
|
sync2.getWaitingThreads(c); |
745 |
|
shouldThrow(); |
753 |
|
* getWaitingThreads throws IMSE if not synced |
754 |
|
*/ |
755 |
|
public void testGetWaitingThreadsIMSE() { |
756 |
< |
final Mutex sync = new Mutex(); |
756 |
> |
final Mutex sync = new Mutex(); |
757 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = (sync.newCondition()); |
758 |
|
try { |
759 |
|
sync.getWaitingThreads(c); |
770 |
|
* hasWaiters returns true when a thread is waiting, else false |
771 |
|
*/ |
772 |
|
public void testHasWaiters() { |
773 |
< |
final Mutex sync = new Mutex(); |
773 |
> |
final Mutex sync = new Mutex(); |
774 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
775 |
< |
Thread t = new Thread(new Runnable() { |
775 |
> |
Thread t = new Thread(new Runnable() { |
776 |
|
public void run() { |
777 |
|
try { |
778 |
|
sync.acquire(1); |
781 |
|
c.await(); |
782 |
|
sync.release(1); |
783 |
|
} |
784 |
< |
catch(InterruptedException e) { |
784 |
> |
catch (InterruptedException e) { |
785 |
|
threadUnexpectedException(); |
786 |
|
} |
787 |
|
} |
812 |
|
* getWaitQueueLength returns number of waiting threads |
813 |
|
*/ |
814 |
|
public void testGetWaitQueueLength() { |
815 |
< |
final Mutex sync = new Mutex(); |
815 |
> |
final Mutex sync = new Mutex(); |
816 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
817 |
< |
Thread t1 = new Thread(new Runnable() { |
817 |
> |
Thread t1 = new Thread(new Runnable() { |
818 |
|
public void run() { |
819 |
|
try { |
820 |
|
sync.acquire(1); |
823 |
|
c.await(); |
824 |
|
sync.release(1); |
825 |
|
} |
826 |
< |
catch(InterruptedException e) { |
826 |
> |
catch (InterruptedException e) { |
827 |
|
threadUnexpectedException(); |
828 |
|
} |
829 |
|
} |
830 |
|
}); |
831 |
|
|
832 |
< |
Thread t2 = new Thread(new Runnable() { |
832 |
> |
Thread t2 = new Thread(new Runnable() { |
833 |
|
public void run() { |
834 |
|
try { |
835 |
|
sync.acquire(1); |
838 |
|
c.await(); |
839 |
|
sync.release(1); |
840 |
|
} |
841 |
< |
catch(InterruptedException e) { |
841 |
> |
catch (InterruptedException e) { |
842 |
|
threadUnexpectedException(); |
843 |
|
} |
844 |
|
} |
873 |
|
* getWaitingThreads returns only and all waiting threads |
874 |
|
*/ |
875 |
|
public void testGetWaitingThreads() { |
876 |
< |
final Mutex sync = new Mutex(); |
876 |
> |
final Mutex sync = new Mutex(); |
877 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
878 |
< |
Thread t1 = new Thread(new Runnable() { |
878 |
> |
Thread t1 = new Thread(new Runnable() { |
879 |
|
public void run() { |
880 |
|
try { |
881 |
|
sync.acquire(1); |
883 |
|
c.await(); |
884 |
|
sync.release(1); |
885 |
|
} |
886 |
< |
catch(InterruptedException e) { |
886 |
> |
catch (InterruptedException e) { |
887 |
|
threadUnexpectedException(); |
888 |
|
} |
889 |
|
} |
890 |
|
}); |
891 |
|
|
892 |
< |
Thread t2 = new Thread(new Runnable() { |
892 |
> |
Thread t2 = new Thread(new Runnable() { |
893 |
|
public void run() { |
894 |
|
try { |
895 |
|
sync.acquire(1); |
897 |
|
c.await(); |
898 |
|
sync.release(1); |
899 |
|
} |
900 |
< |
catch(InterruptedException e) { |
900 |
> |
catch (InterruptedException e) { |
901 |
|
threadUnexpectedException(); |
902 |
|
} |
903 |
|
} |
938 |
|
* awaitUninterruptibly doesn't abort on interrupt |
939 |
|
*/ |
940 |
|
public void testAwaitUninterruptibly() { |
941 |
< |
final Mutex sync = new Mutex(); |
941 |
> |
final Mutex sync = new Mutex(); |
942 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
943 |
< |
Thread t = new Thread(new Runnable() { |
943 |
> |
Thread t = new Thread(new Runnable() { |
944 |
|
public void run() { |
945 |
|
sync.acquire(1); |
946 |
|
c.awaitUninterruptibly(); |
967 |
|
* await is interruptible |
968 |
|
*/ |
969 |
|
public void testAwait_Interrupt() { |
970 |
< |
final Mutex sync = new Mutex(); |
970 |
> |
final Mutex sync = new Mutex(); |
971 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
972 |
< |
Thread t = new Thread(new Runnable() { |
972 |
> |
Thread t = new Thread(new Runnable() { |
973 |
|
public void run() { |
974 |
|
try { |
975 |
|
sync.acquire(1); |
977 |
|
sync.release(1); |
978 |
|
threadShouldThrow(); |
979 |
|
} |
980 |
< |
catch(InterruptedException success) { |
980 |
> |
catch (InterruptedException success) { |
981 |
|
} |
982 |
|
} |
983 |
|
}); |
998 |
|
* awaitNanos is interruptible |
999 |
|
*/ |
1000 |
|
public void testAwaitNanos_Interrupt() { |
1001 |
< |
final Mutex sync = new Mutex(); |
1001 |
> |
final Mutex sync = new Mutex(); |
1002 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1003 |
< |
Thread t = new Thread(new Runnable() { |
1003 |
> |
Thread t = new Thread(new Runnable() { |
1004 |
|
public void run() { |
1005 |
|
try { |
1006 |
|
sync.acquire(1); |
1008 |
|
sync.release(1); |
1009 |
|
threadShouldThrow(); |
1010 |
|
} |
1011 |
< |
catch(InterruptedException success) { |
1011 |
> |
catch (InterruptedException success) { |
1012 |
|
} |
1013 |
|
} |
1014 |
|
}); |
1029 |
|
* awaitUntil is interruptible |
1030 |
|
*/ |
1031 |
|
public void testAwaitUntil_Interrupt() { |
1032 |
< |
final Mutex sync = new Mutex(); |
1032 |
> |
final Mutex sync = new Mutex(); |
1033 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1034 |
< |
Thread t = new Thread(new Runnable() { |
1034 |
> |
Thread t = new Thread(new Runnable() { |
1035 |
|
public void run() { |
1036 |
|
try { |
1037 |
|
sync.acquire(1); |
1040 |
|
sync.release(1); |
1041 |
|
threadShouldThrow(); |
1042 |
|
} |
1043 |
< |
catch(InterruptedException success) { |
1043 |
> |
catch (InterruptedException success) { |
1044 |
|
} |
1045 |
|
} |
1046 |
|
}); |
1061 |
|
* signalAll wakes up all threads |
1062 |
|
*/ |
1063 |
|
public void testSignalAll() { |
1064 |
< |
final Mutex sync = new Mutex(); |
1064 |
> |
final Mutex sync = new Mutex(); |
1065 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
1066 |
< |
Thread t1 = new Thread(new Runnable() { |
1066 |
> |
Thread t1 = new Thread(new Runnable() { |
1067 |
|
public void run() { |
1068 |
|
try { |
1069 |
|
sync.acquire(1); |
1070 |
|
c.await(); |
1071 |
|
sync.release(1); |
1072 |
|
} |
1073 |
< |
catch(InterruptedException e) { |
1073 |
> |
catch (InterruptedException e) { |
1074 |
|
threadUnexpectedException(); |
1075 |
|
} |
1076 |
|
} |
1077 |
|
}); |
1078 |
|
|
1079 |
< |
Thread t2 = new Thread(new Runnable() { |
1079 |
> |
Thread t2 = new Thread(new Runnable() { |
1080 |
|
public void run() { |
1081 |
|
try { |
1082 |
|
sync.acquire(1); |
1083 |
|
c.await(); |
1084 |
|
sync.release(1); |
1085 |
|
} |
1086 |
< |
catch(InterruptedException e) { |
1086 |
> |
catch (InterruptedException e) { |
1087 |
|
threadUnexpectedException(); |
1088 |
|
} |
1089 |
|
} |
1137 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1138 |
|
Mutex r = (Mutex) in.readObject(); |
1139 |
|
assertTrue(r.isHeldExclusively()); |
1140 |
< |
} catch(Exception e){ |
1140 |
> |
} catch (Exception e) { |
1141 |
|
e.printStackTrace(); |
1142 |
|
unexpectedException(); |
1143 |
|
} |
1178 |
|
threadAssertFalse(l.isSignalled()); |
1179 |
|
l.acquireSharedInterruptibly(0); |
1180 |
|
threadAssertTrue(l.isSignalled()); |
1181 |
< |
} catch(InterruptedException e){ |
1181 |
> |
} catch (InterruptedException e) { |
1182 |
|
threadUnexpectedException(); |
1183 |
|
} |
1184 |
|
} |
1190 |
|
l.releaseShared(0); |
1191 |
|
assertTrue(l.isSignalled()); |
1192 |
|
t.join(); |
1193 |
< |
} catch (InterruptedException e){ |
1193 |
> |
} catch (InterruptedException e) { |
1194 |
|
unexpectedException(); |
1195 |
|
} |
1196 |
|
} |
1197 |
< |
|
1197 |
> |
|
1198 |
|
|
1199 |
|
/** |
1200 |
|
* acquireSharedTimed returns after release |
1209 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1210 |
|
threadAssertTrue(l.isSignalled()); |
1211 |
|
|
1212 |
< |
} catch(InterruptedException e){ |
1212 |
> |
} catch (InterruptedException e) { |
1213 |
|
threadUnexpectedException(); |
1214 |
|
} |
1215 |
|
} |
1221 |
|
l.releaseShared(0); |
1222 |
|
assertTrue(l.isSignalled()); |
1223 |
|
t.join(); |
1224 |
< |
} catch (InterruptedException e){ |
1224 |
> |
} catch (InterruptedException e) { |
1225 |
|
unexpectedException(); |
1226 |
|
} |
1227 |
|
} |
1228 |
< |
|
1228 |
> |
|
1229 |
|
/** |
1230 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1231 |
|
*/ |
1237 |
|
threadAssertFalse(l.isSignalled()); |
1238 |
|
l.acquireSharedInterruptibly(0); |
1239 |
|
threadShouldThrow(); |
1240 |
< |
} catch(InterruptedException success){} |
1240 |
> |
} catch (InterruptedException success) {} |
1241 |
|
} |
1242 |
|
}); |
1243 |
|
t.start(); |
1245 |
|
assertFalse(l.isSignalled()); |
1246 |
|
t.interrupt(); |
1247 |
|
t.join(); |
1248 |
< |
} catch (InterruptedException e){ |
1248 |
> |
} catch (InterruptedException e) { |
1249 |
|
unexpectedException(); |
1250 |
|
} |
1251 |
|
} |
1260 |
|
try { |
1261 |
|
threadAssertFalse(l.isSignalled()); |
1262 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1263 |
< |
threadShouldThrow(); |
1264 |
< |
} catch(InterruptedException success){} |
1263 |
> |
threadShouldThrow(); |
1264 |
> |
} catch (InterruptedException success) {} |
1265 |
|
} |
1266 |
|
}); |
1267 |
|
t.start(); |
1270 |
|
assertFalse(l.isSignalled()); |
1271 |
|
t.interrupt(); |
1272 |
|
t.join(); |
1273 |
< |
} catch (InterruptedException e){ |
1273 |
> |
} catch (InterruptedException e) { |
1274 |
|
unexpectedException(); |
1275 |
|
} |
1276 |
|
} |
1285 |
|
try { |
1286 |
|
threadAssertFalse(l.isSignalled()); |
1287 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1288 |
< |
} catch(InterruptedException ie){ |
1288 |
> |
} catch (InterruptedException ie) { |
1289 |
|
threadUnexpectedException(); |
1290 |
|
} |
1291 |
|
} |
1295 |
|
Thread.sleep(SHORT_DELAY_MS); |
1296 |
|
assertFalse(l.isSignalled()); |
1297 |
|
t.join(); |
1298 |
< |
} catch (InterruptedException e){ |
1298 |
> |
} catch (InterruptedException e) { |
1299 |
|
unexpectedException(); |
1300 |
|
} |
1301 |
|
} |
1302 |
|
|
1303 |
< |
|
1303 |
> |
|
1304 |
|
} |