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 |
|
|
31 |
|
*/ |
32 |
|
static class Mutex extends AbstractQueuedSynchronizer { |
33 |
|
public boolean isHeldExclusively() { return getState() == 1; } |
34 |
< |
|
34 |
> |
|
35 |
|
public boolean tryAcquire(int acquires) { |
36 |
< |
assertTrue(acquires == 1); |
36 |
> |
assertTrue(acquires == 1); |
37 |
|
return compareAndSetState(0, 1); |
38 |
|
} |
39 |
< |
|
39 |
> |
|
40 |
|
public boolean tryRelease(int releases) { |
41 |
|
if (getState() == 0) throw new IllegalMonitorStateException(); |
42 |
|
setState(0); |
43 |
|
return true; |
44 |
|
} |
45 |
< |
|
45 |
> |
|
46 |
|
public AbstractQueuedSynchronizer.ConditionObject newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } |
47 |
|
|
48 |
|
} |
49 |
|
|
50 |
< |
|
50 |
> |
|
51 |
|
/** |
52 |
|
* A simple latch class, to test shared mode. |
53 |
|
*/ |
54 |
< |
static class BooleanLatch extends AbstractQueuedSynchronizer { |
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 |
|
} |
60 |
< |
|
60 |
> |
|
61 |
|
public boolean tryReleaseShared(int ignore) { |
62 |
|
setState(1); |
63 |
|
return true; |
73 |
|
public void run() { |
74 |
|
try { |
75 |
|
sync.acquireInterruptibly(1); |
76 |
< |
} catch(InterruptedException success){} |
76 |
> |
} catch (InterruptedException success){} |
77 |
|
} |
78 |
|
} |
79 |
|
|
89 |
|
try { |
90 |
|
sync.acquireInterruptibly(1); |
91 |
|
threadShouldThrow(); |
92 |
< |
} catch(InterruptedException success){} |
92 |
> |
} catch (InterruptedException success){} |
93 |
|
} |
94 |
|
} |
95 |
|
|
96 |
|
/** |
97 |
|
* isHeldExclusively is false upon construction |
98 |
|
*/ |
99 |
< |
public void testIsHeldExclusively() { |
99 |
> |
public void testIsHeldExclusively() { |
100 |
|
Mutex rl = new Mutex(); |
101 |
|
assertFalse(rl.isHeldExclusively()); |
102 |
|
} |
103 |
< |
|
103 |
> |
|
104 |
|
/** |
105 |
|
* acquiring released sync succeeds |
106 |
|
*/ |
107 |
< |
public void testAcquire() { |
107 |
> |
public void testAcquire() { |
108 |
|
Mutex rl = new Mutex(); |
109 |
|
rl.acquire(1); |
110 |
|
assertTrue(rl.isHeldExclusively()); |
115 |
|
/** |
116 |
|
* tryAcquire on an released sync succeeds |
117 |
|
*/ |
118 |
< |
public void testTryAcquire() { |
118 |
> |
public void testTryAcquire() { |
119 |
|
Mutex rl = new Mutex(); |
120 |
|
assertTrue(rl.tryAcquire(1)); |
121 |
|
assertTrue(rl.isHeldExclusively()); |
125 |
|
/** |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
< |
public void testhasQueuedThreads() { |
128 |
> |
public void testhasQueuedThreads() { |
129 |
|
final Mutex sync = new Mutex(); |
130 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
146 |
|
assertFalse(sync.hasQueuedThreads()); |
147 |
|
t1.join(); |
148 |
|
t2.join(); |
149 |
< |
} catch(Exception e){ |
149 |
> |
} catch (Exception e){ |
150 |
|
unexpectedException(); |
151 |
|
} |
152 |
< |
} |
152 |
> |
} |
153 |
|
|
154 |
|
/** |
155 |
|
* isQueued(null) throws NPE |
156 |
|
*/ |
157 |
< |
public void testIsQueuedNPE() { |
157 |
> |
public void testIsQueuedNPE() { |
158 |
|
final Mutex sync = new Mutex(); |
159 |
|
try { |
160 |
|
sync.isQueued(null); |
166 |
|
/** |
167 |
|
* isQueued reports whether a thread is queued. |
168 |
|
*/ |
169 |
< |
public void testIsQueued() { |
169 |
> |
public void testIsQueued() { |
170 |
|
final Mutex sync = new Mutex(); |
171 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
172 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
192 |
|
assertFalse(sync.isQueued(t2)); |
193 |
|
t1.join(); |
194 |
|
t2.join(); |
195 |
< |
} catch(Exception e){ |
195 |
> |
} catch (Exception e){ |
196 |
|
unexpectedException(); |
197 |
|
} |
198 |
< |
} |
198 |
> |
} |
199 |
|
|
200 |
|
/** |
201 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
202 |
|
*/ |
203 |
< |
public void testGetFirstQueuedThread() { |
203 |
> |
public void testGetFirstQueuedThread() { |
204 |
|
final Mutex sync = new Mutex(); |
205 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
206 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
222 |
|
assertNull(sync.getFirstQueuedThread()); |
223 |
|
t1.join(); |
224 |
|
t2.join(); |
225 |
< |
} catch(Exception e){ |
225 |
> |
} catch (Exception e){ |
226 |
|
unexpectedException(); |
227 |
|
} |
228 |
< |
} |
228 |
> |
} |
229 |
|
|
230 |
|
|
231 |
|
/** |
232 |
|
* hasContended reports false if no thread has ever blocked, else true |
233 |
|
*/ |
234 |
< |
public void testHasContended() { |
234 |
> |
public void testHasContended() { |
235 |
|
final Mutex sync = new Mutex(); |
236 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
237 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
|
assertTrue(sync.hasContended()); |
253 |
|
t1.join(); |
254 |
|
t2.join(); |
255 |
< |
} catch(Exception e){ |
255 |
> |
} catch (Exception e){ |
256 |
|
unexpectedException(); |
257 |
|
} |
258 |
< |
} |
258 |
> |
} |
259 |
|
|
260 |
|
/** |
261 |
|
* getQueuedThreads includes waiting threads |
262 |
|
*/ |
263 |
< |
public void testGetQueuedThreads() { |
263 |
> |
public void testGetQueuedThreads() { |
264 |
|
final Mutex sync = new Mutex(); |
265 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
266 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
284 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
285 |
|
t1.join(); |
286 |
|
t2.join(); |
287 |
< |
} catch(Exception e){ |
287 |
> |
} catch (Exception e){ |
288 |
|
unexpectedException(); |
289 |
|
} |
290 |
< |
} |
290 |
> |
} |
291 |
|
|
292 |
|
/** |
293 |
|
* getExclusiveQueuedThreads includes waiting threads |
294 |
|
*/ |
295 |
< |
public void testGetExclusiveQueuedThreads() { |
295 |
> |
public void testGetExclusiveQueuedThreads() { |
296 |
|
final Mutex sync = new Mutex(); |
297 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
298 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
316 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
317 |
|
t1.join(); |
318 |
|
t2.join(); |
319 |
< |
} catch(Exception e){ |
319 |
> |
} catch (Exception e){ |
320 |
|
unexpectedException(); |
321 |
|
} |
322 |
< |
} |
322 |
> |
} |
323 |
|
|
324 |
|
/** |
325 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
326 |
|
*/ |
327 |
< |
public void testGetSharedQueuedThreads() { |
327 |
> |
public void testGetSharedQueuedThreads() { |
328 |
|
final Mutex sync = new Mutex(); |
329 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
330 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
346 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
347 |
|
t1.join(); |
348 |
|
t2.join(); |
349 |
< |
} catch(Exception e){ |
349 |
> |
} catch (Exception e){ |
350 |
|
unexpectedException(); |
351 |
|
} |
352 |
< |
} |
352 |
> |
} |
353 |
|
|
354 |
|
/** |
355 |
|
* tryAcquireNanos is interruptible. |
356 |
|
*/ |
357 |
< |
public void testInterruptedException2() { |
357 |
> |
public void testInterruptedException2() { |
358 |
|
final Mutex sync = new Mutex(); |
359 |
|
sync.acquire(1); |
360 |
|
Thread t = new Thread(new Runnable() { |
362 |
|
try { |
363 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
364 |
|
threadShouldThrow(); |
365 |
< |
} catch(InterruptedException success){} |
365 |
> |
} catch (InterruptedException success){} |
366 |
|
} |
367 |
|
}); |
368 |
|
try { |
369 |
|
t.start(); |
370 |
|
t.interrupt(); |
371 |
< |
} catch(Exception e){ |
371 |
> |
} catch (Exception e){ |
372 |
|
unexpectedException(); |
373 |
|
} |
374 |
|
} |
377 |
|
/** |
378 |
|
* TryAcquire on exclusively held sync fails |
379 |
|
*/ |
380 |
< |
public void testTryAcquireWhenSynced() { |
380 |
> |
public void testTryAcquireWhenSynced() { |
381 |
|
final Mutex sync = new Mutex(); |
382 |
|
sync.acquire(1); |
383 |
|
Thread t = new Thread(new Runnable() { |
389 |
|
t.start(); |
390 |
|
t.join(); |
391 |
|
sync.release(1); |
392 |
< |
} catch(Exception e){ |
392 |
> |
} catch (Exception e){ |
393 |
|
unexpectedException(); |
394 |
|
} |
395 |
< |
} |
395 |
> |
} |
396 |
|
|
397 |
|
/** |
398 |
|
* tryAcquireNanos on an exclusively held sync times out |
399 |
|
*/ |
400 |
< |
public void testAcquireNanos_Timeout() { |
400 |
> |
public void testAcquireNanos_Timeout() { |
401 |
|
final Mutex sync = new Mutex(); |
402 |
|
sync.acquire(1); |
403 |
|
Thread t = new Thread(new Runnable() { |
413 |
|
t.start(); |
414 |
|
t.join(); |
415 |
|
sync.release(1); |
416 |
< |
} catch(Exception e){ |
416 |
> |
} catch (Exception e){ |
417 |
|
unexpectedException(); |
418 |
|
} |
419 |
< |
} |
420 |
< |
|
421 |
< |
|
419 |
> |
} |
420 |
> |
|
421 |
> |
|
422 |
|
/** |
423 |
|
* getState is true when acquired and false when not |
424 |
|
*/ |
428 |
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
sync.release(1); |
430 |
|
assertFalse(sync.isHeldExclusively()); |
431 |
< |
Thread t = new Thread(new Runnable() { |
431 |
> |
Thread t = new Thread(new Runnable() { |
432 |
|
public void run() { |
433 |
|
sync.acquire(1); |
434 |
|
try { |
435 |
|
Thread.sleep(SMALL_DELAY_MS); |
436 |
|
} |
437 |
< |
catch(Exception e) { |
437 |
> |
catch (Exception e) { |
438 |
|
threadUnexpectedException(); |
439 |
|
} |
440 |
|
sync.release(1); |
446 |
|
assertTrue(sync.isHeldExclusively()); |
447 |
|
t.join(); |
448 |
|
assertFalse(sync.isHeldExclusively()); |
449 |
< |
} catch(Exception e){ |
449 |
> |
} catch (Exception e){ |
450 |
|
unexpectedException(); |
451 |
|
} |
452 |
|
} |
455 |
|
/** |
456 |
|
* acquireInterruptibly is interruptible. |
457 |
|
*/ |
458 |
< |
public void testAcquireInterruptibly1() { |
458 |
> |
public void testAcquireInterruptibly1() { |
459 |
|
final Mutex sync = new Mutex(); |
460 |
|
sync.acquire(1); |
461 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
462 |
|
try { |
463 |
|
t.start(); |
464 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
465 |
|
t.interrupt(); |
466 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
467 |
|
sync.release(1); |
468 |
|
t.join(); |
469 |
< |
} catch(Exception e){ |
469 |
> |
} catch (Exception e){ |
470 |
|
unexpectedException(); |
471 |
|
} |
472 |
< |
} |
472 |
> |
} |
473 |
|
|
474 |
|
/** |
475 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
476 |
|
*/ |
477 |
|
public void testAcquireInterruptibly2() { |
478 |
< |
final Mutex sync = new Mutex(); |
478 |
> |
final Mutex sync = new Mutex(); |
479 |
|
try { |
480 |
|
sync.acquireInterruptibly(1); |
481 |
< |
} catch(Exception e) { |
481 |
> |
} catch (Exception e) { |
482 |
|
unexpectedException(); |
483 |
|
} |
484 |
|
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
487 |
|
t.interrupt(); |
488 |
|
assertTrue(sync.isHeldExclusively()); |
489 |
|
t.join(); |
490 |
< |
} catch(Exception e){ |
490 |
> |
} catch (Exception e){ |
491 |
|
unexpectedException(); |
492 |
|
} |
493 |
|
} |
496 |
|
* owns is true for a condition created by sync else false |
497 |
|
*/ |
498 |
|
public void testOwns() { |
499 |
< |
final Mutex sync = new Mutex(); |
499 |
> |
final Mutex sync = new Mutex(); |
500 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
501 |
|
final Mutex sync2 = new Mutex(); |
502 |
|
assertTrue(sync.owns(c)); |
507 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
508 |
|
*/ |
509 |
|
public void testAwait_IllegalMonitor() { |
510 |
< |
final Mutex sync = new Mutex(); |
510 |
> |
final Mutex sync = new Mutex(); |
511 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
512 |
|
try { |
513 |
|
c.await(); |
524 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
525 |
|
*/ |
526 |
|
public void testSignal_IllegalMonitor() { |
527 |
< |
final Mutex sync = new Mutex(); |
527 |
> |
final Mutex sync = new Mutex(); |
528 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
529 |
|
try { |
530 |
|
c.signal(); |
541 |
|
* awaitNanos without a signal times out |
542 |
|
*/ |
543 |
|
public void testAwaitNanos_Timeout() { |
544 |
< |
final Mutex sync = new Mutex(); |
544 |
> |
final Mutex sync = new Mutex(); |
545 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
546 |
|
try { |
547 |
|
sync.acquire(1); |
558 |
|
* Timed await without a signal times out |
559 |
|
*/ |
560 |
|
public void testAwait_Timeout() { |
561 |
< |
final Mutex sync = new Mutex(); |
561 |
> |
final Mutex sync = new Mutex(); |
562 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
563 |
|
try { |
564 |
|
sync.acquire(1); |
574 |
|
* awaitUntil without a signal times out |
575 |
|
*/ |
576 |
|
public void testAwaitUntil_Timeout() { |
577 |
< |
final Mutex sync = new Mutex(); |
577 |
> |
final Mutex sync = new Mutex(); |
578 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
579 |
|
try { |
580 |
|
sync.acquire(1); |
591 |
|
* await returns when signalled |
592 |
|
*/ |
593 |
|
public void testAwait() { |
594 |
< |
final Mutex sync = new Mutex(); |
594 |
> |
final Mutex sync = new Mutex(); |
595 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
596 |
< |
Thread t = new Thread(new Runnable() { |
596 |
> |
Thread t = new Thread(new Runnable() { |
597 |
|
public void run() { |
598 |
|
try { |
599 |
|
sync.acquire(1); |
600 |
|
c.await(); |
601 |
|
sync.release(1); |
602 |
|
} |
603 |
< |
catch(InterruptedException e) { |
603 |
> |
catch (InterruptedException e) { |
604 |
|
threadUnexpectedException(); |
605 |
|
} |
606 |
|
} |
734 |
|
* getWaitingThreads throws IAE if not owned |
735 |
|
*/ |
736 |
|
public void testGetWaitingThreadsIAE() { |
737 |
< |
final Mutex sync = new Mutex(); |
737 |
> |
final Mutex sync = new Mutex(); |
738 |
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
739 |
< |
final Mutex sync2 = new Mutex(); |
739 |
> |
final Mutex sync2 = new Mutex(); |
740 |
|
try { |
741 |
|
sync2.getWaitingThreads(c); |
742 |
|
shouldThrow(); |
750 |
|
* getWaitingThreads throws IMSE if not synced |
751 |
|
*/ |
752 |
|
public void testGetWaitingThreadsIMSE() { |
753 |
< |
final Mutex sync = new Mutex(); |
753 |
> |
final Mutex sync = new Mutex(); |
754 |
|
final AbstractQueuedSynchronizer.ConditionObject c = (sync.newCondition()); |
755 |
|
try { |
756 |
|
sync.getWaitingThreads(c); |
767 |
|
* hasWaiters returns true when a thread is waiting, else false |
768 |
|
*/ |
769 |
|
public void testHasWaiters() { |
770 |
< |
final Mutex sync = new Mutex(); |
770 |
> |
final Mutex sync = new Mutex(); |
771 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
772 |
< |
Thread t = new Thread(new Runnable() { |
772 |
> |
Thread t = new Thread(new Runnable() { |
773 |
|
public void run() { |
774 |
|
try { |
775 |
|
sync.acquire(1); |
778 |
|
c.await(); |
779 |
|
sync.release(1); |
780 |
|
} |
781 |
< |
catch(InterruptedException e) { |
781 |
> |
catch (InterruptedException e) { |
782 |
|
threadUnexpectedException(); |
783 |
|
} |
784 |
|
} |
809 |
|
* getWaitQueueLength returns number of waiting threads |
810 |
|
*/ |
811 |
|
public void testGetWaitQueueLength() { |
812 |
< |
final Mutex sync = new Mutex(); |
812 |
> |
final Mutex sync = new Mutex(); |
813 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
814 |
< |
Thread t1 = new Thread(new Runnable() { |
814 |
> |
Thread t1 = new Thread(new Runnable() { |
815 |
|
public void run() { |
816 |
|
try { |
817 |
|
sync.acquire(1); |
820 |
|
c.await(); |
821 |
|
sync.release(1); |
822 |
|
} |
823 |
< |
catch(InterruptedException e) { |
823 |
> |
catch (InterruptedException e) { |
824 |
|
threadUnexpectedException(); |
825 |
|
} |
826 |
|
} |
827 |
|
}); |
828 |
|
|
829 |
< |
Thread t2 = new Thread(new Runnable() { |
829 |
> |
Thread t2 = new Thread(new Runnable() { |
830 |
|
public void run() { |
831 |
|
try { |
832 |
|
sync.acquire(1); |
835 |
|
c.await(); |
836 |
|
sync.release(1); |
837 |
|
} |
838 |
< |
catch(InterruptedException e) { |
838 |
> |
catch (InterruptedException e) { |
839 |
|
threadUnexpectedException(); |
840 |
|
} |
841 |
|
} |
870 |
|
* getWaitingThreads returns only and all waiting threads |
871 |
|
*/ |
872 |
|
public void testGetWaitingThreads() { |
873 |
< |
final Mutex sync = new Mutex(); |
873 |
> |
final Mutex sync = new Mutex(); |
874 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
875 |
< |
Thread t1 = new Thread(new Runnable() { |
875 |
> |
Thread t1 = new Thread(new Runnable() { |
876 |
|
public void run() { |
877 |
|
try { |
878 |
|
sync.acquire(1); |
880 |
|
c.await(); |
881 |
|
sync.release(1); |
882 |
|
} |
883 |
< |
catch(InterruptedException e) { |
883 |
> |
catch (InterruptedException e) { |
884 |
|
threadUnexpectedException(); |
885 |
|
} |
886 |
|
} |
887 |
|
}); |
888 |
|
|
889 |
< |
Thread t2 = new Thread(new Runnable() { |
889 |
> |
Thread t2 = new Thread(new Runnable() { |
890 |
|
public void run() { |
891 |
|
try { |
892 |
|
sync.acquire(1); |
894 |
|
c.await(); |
895 |
|
sync.release(1); |
896 |
|
} |
897 |
< |
catch(InterruptedException e) { |
897 |
> |
catch (InterruptedException e) { |
898 |
|
threadUnexpectedException(); |
899 |
|
} |
900 |
|
} |
935 |
|
* awaitUninterruptibly doesn't abort on interrupt |
936 |
|
*/ |
937 |
|
public void testAwaitUninterruptibly() { |
938 |
< |
final Mutex sync = new Mutex(); |
938 |
> |
final Mutex sync = new Mutex(); |
939 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
940 |
< |
Thread t = new Thread(new Runnable() { |
940 |
> |
Thread t = new Thread(new Runnable() { |
941 |
|
public void run() { |
942 |
|
sync.acquire(1); |
943 |
|
c.awaitUninterruptibly(); |
964 |
|
* await is interruptible |
965 |
|
*/ |
966 |
|
public void testAwait_Interrupt() { |
967 |
< |
final Mutex sync = new Mutex(); |
967 |
> |
final Mutex sync = new Mutex(); |
968 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
969 |
< |
Thread t = new Thread(new Runnable() { |
969 |
> |
Thread t = new Thread(new Runnable() { |
970 |
|
public void run() { |
971 |
|
try { |
972 |
|
sync.acquire(1); |
974 |
|
sync.release(1); |
975 |
|
threadShouldThrow(); |
976 |
|
} |
977 |
< |
catch(InterruptedException success) { |
977 |
> |
catch (InterruptedException success) { |
978 |
|
} |
979 |
|
} |
980 |
|
}); |
995 |
|
* awaitNanos is interruptible |
996 |
|
*/ |
997 |
|
public void testAwaitNanos_Interrupt() { |
998 |
< |
final Mutex sync = new Mutex(); |
998 |
> |
final Mutex sync = new Mutex(); |
999 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1000 |
< |
Thread t = new Thread(new Runnable() { |
1000 |
> |
Thread t = new Thread(new Runnable() { |
1001 |
|
public void run() { |
1002 |
|
try { |
1003 |
|
sync.acquire(1); |
1005 |
|
sync.release(1); |
1006 |
|
threadShouldThrow(); |
1007 |
|
} |
1008 |
< |
catch(InterruptedException success) { |
1008 |
> |
catch (InterruptedException success) { |
1009 |
|
} |
1010 |
|
} |
1011 |
|
}); |
1026 |
|
* awaitUntil is interruptible |
1027 |
|
*/ |
1028 |
|
public void testAwaitUntil_Interrupt() { |
1029 |
< |
final Mutex sync = new Mutex(); |
1029 |
> |
final Mutex sync = new Mutex(); |
1030 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1031 |
< |
Thread t = new Thread(new Runnable() { |
1031 |
> |
Thread t = new Thread(new Runnable() { |
1032 |
|
public void run() { |
1033 |
|
try { |
1034 |
|
sync.acquire(1); |
1037 |
|
sync.release(1); |
1038 |
|
threadShouldThrow(); |
1039 |
|
} |
1040 |
< |
catch(InterruptedException success) { |
1040 |
> |
catch (InterruptedException success) { |
1041 |
|
} |
1042 |
|
} |
1043 |
|
}); |
1058 |
|
* signalAll wakes up all threads |
1059 |
|
*/ |
1060 |
|
public void testSignalAll() { |
1061 |
< |
final Mutex sync = new Mutex(); |
1061 |
> |
final Mutex sync = new Mutex(); |
1062 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
1063 |
< |
Thread t1 = new Thread(new Runnable() { |
1063 |
> |
Thread t1 = new Thread(new Runnable() { |
1064 |
|
public void run() { |
1065 |
|
try { |
1066 |
|
sync.acquire(1); |
1067 |
|
c.await(); |
1068 |
|
sync.release(1); |
1069 |
|
} |
1070 |
< |
catch(InterruptedException e) { |
1070 |
> |
catch (InterruptedException e) { |
1071 |
|
threadUnexpectedException(); |
1072 |
|
} |
1073 |
|
} |
1074 |
|
}); |
1075 |
|
|
1076 |
< |
Thread t2 = new Thread(new Runnable() { |
1076 |
> |
Thread t2 = new Thread(new Runnable() { |
1077 |
|
public void run() { |
1078 |
|
try { |
1079 |
|
sync.acquire(1); |
1080 |
|
c.await(); |
1081 |
|
sync.release(1); |
1082 |
|
} |
1083 |
< |
catch(InterruptedException e) { |
1083 |
> |
catch (InterruptedException e) { |
1084 |
|
threadUnexpectedException(); |
1085 |
|
} |
1086 |
|
} |
1134 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
1135 |
|
Mutex r = (Mutex) in.readObject(); |
1136 |
|
assertTrue(r.isHeldExclusively()); |
1137 |
< |
} catch(Exception e){ |
1137 |
> |
} catch (Exception e){ |
1138 |
|
e.printStackTrace(); |
1139 |
|
unexpectedException(); |
1140 |
|
} |
1175 |
|
threadAssertFalse(l.isSignalled()); |
1176 |
|
l.acquireSharedInterruptibly(0); |
1177 |
|
threadAssertTrue(l.isSignalled()); |
1178 |
< |
} catch(InterruptedException e){ |
1178 |
> |
} catch (InterruptedException e){ |
1179 |
|
threadUnexpectedException(); |
1180 |
|
} |
1181 |
|
} |
1191 |
|
unexpectedException(); |
1192 |
|
} |
1193 |
|
} |
1194 |
< |
|
1194 |
> |
|
1195 |
|
|
1196 |
|
/** |
1197 |
|
* acquireSharedTimed returns after release |
1206 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
1207 |
|
threadAssertTrue(l.isSignalled()); |
1208 |
|
|
1209 |
< |
} catch(InterruptedException e){ |
1209 |
> |
} catch (InterruptedException e){ |
1210 |
|
threadUnexpectedException(); |
1211 |
|
} |
1212 |
|
} |
1222 |
|
unexpectedException(); |
1223 |
|
} |
1224 |
|
} |
1225 |
< |
|
1225 |
> |
|
1226 |
|
/** |
1227 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
1228 |
|
*/ |
1234 |
|
threadAssertFalse(l.isSignalled()); |
1235 |
|
l.acquireSharedInterruptibly(0); |
1236 |
|
threadShouldThrow(); |
1237 |
< |
} catch(InterruptedException success){} |
1237 |
> |
} catch (InterruptedException success){} |
1238 |
|
} |
1239 |
|
}); |
1240 |
|
t.start(); |
1257 |
|
try { |
1258 |
|
threadAssertFalse(l.isSignalled()); |
1259 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
1260 |
< |
threadShouldThrow(); |
1261 |
< |
} catch(InterruptedException success){} |
1260 |
> |
threadShouldThrow(); |
1261 |
> |
} catch (InterruptedException success){} |
1262 |
|
} |
1263 |
|
}); |
1264 |
|
t.start(); |
1282 |
|
try { |
1283 |
|
threadAssertFalse(l.isSignalled()); |
1284 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1285 |
< |
} catch(InterruptedException ie){ |
1285 |
> |
} catch (InterruptedException ie){ |
1286 |
|
threadUnexpectedException(); |
1287 |
|
} |
1288 |
|
} |
1297 |
|
} |
1298 |
|
} |
1299 |
|
|
1300 |
< |
|
1300 |
> |
|
1301 |
|
} |