10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.util.concurrent.locks.*; |
15 |
|
import java.io.*; |
16 |
|
|
94 |
|
* isHeldExclusively is false upon construction |
95 |
|
*/ |
96 |
|
public void testIsHeldExclusively() { |
97 |
< |
Mutex rl = new Mutex(); |
97 |
> |
Mutex rl = new Mutex(); |
98 |
|
assertFalse(rl.isHeldExclusively()); |
99 |
|
} |
100 |
|
|
102 |
|
* acquiring released sync succeeds |
103 |
|
*/ |
104 |
|
public void testAcquire() { |
105 |
< |
Mutex rl = new Mutex(); |
105 |
> |
Mutex rl = new Mutex(); |
106 |
|
rl.acquire(1); |
107 |
|
assertTrue(rl.isHeldExclusively()); |
108 |
|
rl.release(1); |
113 |
|
* tryAcquire on an released sync succeeds |
114 |
|
*/ |
115 |
|
public void testTryAcquire() { |
116 |
< |
Mutex rl = new Mutex(); |
116 |
> |
Mutex rl = new Mutex(); |
117 |
|
assertTrue(rl.tryAcquire(1)); |
118 |
|
assertTrue(rl.isHeldExclusively()); |
119 |
|
rl.release(1); |
123 |
|
* hasQueuedThreads reports whether there are waiting threads |
124 |
|
*/ |
125 |
|
public void testhasQueuedThreads() throws InterruptedException { |
126 |
< |
final Mutex sync = new Mutex(); |
126 |
> |
final Mutex sync = new Mutex(); |
127 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
128 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
129 |
|
assertFalse(sync.hasQueuedThreads()); |
148 |
|
* isQueued(null) throws NPE |
149 |
|
*/ |
150 |
|
public void testIsQueuedNPE() { |
151 |
< |
final Mutex sync = new Mutex(); |
151 |
> |
final Mutex sync = new Mutex(); |
152 |
|
try { |
153 |
|
sync.isQueued(null); |
154 |
|
shouldThrow(); |
159 |
|
* isQueued reports whether a thread is queued. |
160 |
|
*/ |
161 |
|
public void testIsQueued() throws InterruptedException { |
162 |
< |
final Mutex sync = new Mutex(); |
162 |
> |
final Mutex sync = new Mutex(); |
163 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
164 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
165 |
|
assertFalse(sync.isQueued(t1)); |
189 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
190 |
|
*/ |
191 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
192 |
< |
final Mutex sync = new Mutex(); |
192 |
> |
final Mutex sync = new Mutex(); |
193 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
194 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
195 |
|
assertNull(sync.getFirstQueuedThread()); |
216 |
|
* hasContended reports false if no thread has ever blocked, else true |
217 |
|
*/ |
218 |
|
public void testHasContended() throws InterruptedException { |
219 |
< |
final Mutex sync = new Mutex(); |
219 |
> |
final Mutex sync = new Mutex(); |
220 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
221 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
222 |
|
assertFalse(sync.hasContended()); |
241 |
|
* getQueuedThreads includes waiting threads |
242 |
|
*/ |
243 |
|
public void testGetQueuedThreads() throws InterruptedException { |
244 |
< |
final Mutex sync = new Mutex(); |
244 |
> |
final Mutex sync = new Mutex(); |
245 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
246 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
247 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
269 |
|
* getExclusiveQueuedThreads includes waiting threads |
270 |
|
*/ |
271 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
272 |
< |
final Mutex sync = new Mutex(); |
272 |
> |
final Mutex sync = new Mutex(); |
273 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
274 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
275 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
297 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
298 |
|
*/ |
299 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
300 |
< |
final Mutex sync = new Mutex(); |
300 |
> |
final Mutex sync = new Mutex(); |
301 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
302 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
303 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
323 |
|
* tryAcquireNanos is interruptible. |
324 |
|
*/ |
325 |
|
public void testInterruptedException2() throws InterruptedException { |
326 |
< |
final Mutex sync = new Mutex(); |
327 |
< |
sync.acquire(1); |
328 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
326 |
> |
final Mutex sync = new Mutex(); |
327 |
> |
sync.acquire(1); |
328 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
329 |
|
public void realRun() throws InterruptedException { |
330 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
331 |
|
}}); |
340 |
|
* TryAcquire on exclusively held sync fails |
341 |
|
*/ |
342 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
343 |
< |
final Mutex sync = new Mutex(); |
344 |
< |
sync.acquire(1); |
345 |
< |
Thread t = new Thread(new CheckedRunnable() { |
343 |
> |
final Mutex sync = new Mutex(); |
344 |
> |
sync.acquire(1); |
345 |
> |
Thread t = new Thread(new CheckedRunnable() { |
346 |
|
public void realRun() { |
347 |
|
threadAssertFalse(sync.tryAcquire(1)); |
348 |
|
}}); |
356 |
|
* tryAcquireNanos on an exclusively held sync times out |
357 |
|
*/ |
358 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
359 |
< |
final Mutex sync = new Mutex(); |
360 |
< |
sync.acquire(1); |
361 |
< |
Thread t = new Thread(new CheckedRunnable() { |
359 |
> |
final Mutex sync = new Mutex(); |
360 |
> |
sync.acquire(1); |
361 |
> |
Thread t = new Thread(new CheckedRunnable() { |
362 |
|
public void realRun() throws InterruptedException { |
363 |
|
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
364 |
|
}}); |
373 |
|
* getState is true when acquired and false when not |
374 |
|
*/ |
375 |
|
public void testGetState() throws InterruptedException { |
376 |
< |
final Mutex sync = new Mutex(); |
377 |
< |
sync.acquire(1); |
378 |
< |
assertTrue(sync.isHeldExclusively()); |
379 |
< |
sync.release(1); |
380 |
< |
assertFalse(sync.isHeldExclusively()); |
381 |
< |
Thread t = new Thread(new CheckedRunnable() { |
376 |
> |
final Mutex sync = new Mutex(); |
377 |
> |
sync.acquire(1); |
378 |
> |
assertTrue(sync.isHeldExclusively()); |
379 |
> |
sync.release(1); |
380 |
> |
assertFalse(sync.isHeldExclusively()); |
381 |
> |
Thread t = new Thread(new CheckedRunnable() { |
382 |
|
public void realRun() throws InterruptedException { |
383 |
|
sync.acquire(1); |
384 |
|
Thread.sleep(SMALL_DELAY_MS); |
397 |
|
* acquireInterruptibly is interruptible. |
398 |
|
*/ |
399 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
400 |
< |
final Mutex sync = new Mutex(); |
401 |
< |
sync.acquire(1); |
402 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
400 |
> |
final Mutex sync = new Mutex(); |
401 |
> |
sync.acquire(1); |
402 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
403 |
|
|
404 |
|
t.start(); |
405 |
|
Thread.sleep(SHORT_DELAY_MS); |
413 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
414 |
|
*/ |
415 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
416 |
< |
final Mutex sync = new Mutex(); |
416 |
> |
final Mutex sync = new Mutex(); |
417 |
|
sync.acquireInterruptibly(1); |
418 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
418 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
419 |
|
t.start(); |
420 |
|
t.interrupt(); |
421 |
|
assertTrue(sync.isHeldExclusively()); |
426 |
|
* owns is true for a condition created by sync else false |
427 |
|
*/ |
428 |
|
public void testOwns() { |
429 |
< |
final Mutex sync = new Mutex(); |
429 |
> |
final Mutex sync = new Mutex(); |
430 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
431 |
|
final Mutex sync2 = new Mutex(); |
432 |
|
assertTrue(sync.owns(c)); |
437 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
438 |
|
*/ |
439 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
440 |
< |
final Mutex sync = new Mutex(); |
440 |
> |
final Mutex sync = new Mutex(); |
441 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
442 |
|
try { |
443 |
|
c.await(); |
449 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
450 |
|
*/ |
451 |
|
public void testSignal_IllegalMonitor() { |
452 |
< |
final Mutex sync = new Mutex(); |
452 |
> |
final Mutex sync = new Mutex(); |
453 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
454 |
|
try { |
455 |
|
c.signal(); |
461 |
|
* awaitNanos without a signal times out |
462 |
|
*/ |
463 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
464 |
< |
final Mutex sync = new Mutex(); |
464 |
> |
final Mutex sync = new Mutex(); |
465 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
466 |
|
sync.acquire(1); |
467 |
|
long t = c.awaitNanos(100); |
473 |
|
* Timed await without a signal times out |
474 |
|
*/ |
475 |
|
public void testAwait_Timeout() throws InterruptedException { |
476 |
< |
final Mutex sync = new Mutex(); |
476 |
> |
final Mutex sync = new Mutex(); |
477 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
478 |
|
sync.acquire(1); |
479 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
479 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
480 |
|
sync.release(1); |
481 |
|
} |
482 |
|
|
484 |
|
* awaitUntil without a signal times out |
485 |
|
*/ |
486 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
487 |
< |
final Mutex sync = new Mutex(); |
487 |
> |
final Mutex sync = new Mutex(); |
488 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
489 |
|
sync.acquire(1); |
490 |
|
java.util.Date d = new java.util.Date(); |
496 |
|
* await returns when signalled |
497 |
|
*/ |
498 |
|
public void testAwait() throws InterruptedException { |
499 |
< |
final Mutex sync = new Mutex(); |
499 |
> |
final Mutex sync = new Mutex(); |
500 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
501 |
< |
Thread t = new Thread(new CheckedRunnable() { |
501 |
> |
Thread t = new Thread(new CheckedRunnable() { |
502 |
|
public void realRun() throws InterruptedException { |
503 |
|
sync.acquire(1); |
504 |
|
c.await(); |
520 |
|
* hasWaiters throws NPE if null |
521 |
|
*/ |
522 |
|
public void testHasWaitersNPE() { |
523 |
< |
final Mutex sync = new Mutex(); |
523 |
> |
final Mutex sync = new Mutex(); |
524 |
|
try { |
525 |
|
sync.hasWaiters(null); |
526 |
|
shouldThrow(); |
531 |
|
* getWaitQueueLength throws NPE if null |
532 |
|
*/ |
533 |
|
public void testGetWaitQueueLengthNPE() { |
534 |
< |
final Mutex sync = new Mutex(); |
534 |
> |
final Mutex sync = new Mutex(); |
535 |
|
try { |
536 |
|
sync.getWaitQueueLength(null); |
537 |
|
shouldThrow(); |
543 |
|
* getWaitingThreads throws NPE if null |
544 |
|
*/ |
545 |
|
public void testGetWaitingThreadsNPE() { |
546 |
< |
final Mutex sync = new Mutex(); |
546 |
> |
final Mutex sync = new Mutex(); |
547 |
|
try { |
548 |
|
sync.getWaitingThreads(null); |
549 |
|
shouldThrow(); |
555 |
|
* hasWaiters throws IAE if not owned |
556 |
|
*/ |
557 |
|
public void testHasWaitersIAE() { |
558 |
< |
final Mutex sync = new Mutex(); |
558 |
> |
final Mutex sync = new Mutex(); |
559 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
560 |
< |
final Mutex sync2 = new Mutex(); |
560 |
> |
final Mutex sync2 = new Mutex(); |
561 |
|
try { |
562 |
|
sync2.hasWaiters(c); |
563 |
|
shouldThrow(); |
568 |
|
* hasWaiters throws IMSE if not synced |
569 |
|
*/ |
570 |
|
public void testHasWaitersIMSE() { |
571 |
< |
final Mutex sync = new Mutex(); |
571 |
> |
final Mutex sync = new Mutex(); |
572 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
573 |
|
try { |
574 |
|
sync.hasWaiters(c); |
581 |
|
* getWaitQueueLength throws IAE if not owned |
582 |
|
*/ |
583 |
|
public void testGetWaitQueueLengthIAE() { |
584 |
< |
final Mutex sync = new Mutex(); |
584 |
> |
final Mutex sync = new Mutex(); |
585 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
586 |
< |
final Mutex sync2 = new Mutex(); |
586 |
> |
final Mutex sync2 = new Mutex(); |
587 |
|
try { |
588 |
|
sync2.getWaitQueueLength(c); |
589 |
|
shouldThrow(); |
594 |
|
* getWaitQueueLength throws IMSE if not synced |
595 |
|
*/ |
596 |
|
public void testGetWaitQueueLengthIMSE() { |
597 |
< |
final Mutex sync = new Mutex(); |
597 |
> |
final Mutex sync = new Mutex(); |
598 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
599 |
|
try { |
600 |
|
sync.getWaitQueueLength(c); |
607 |
|
* getWaitingThreads throws IAE if not owned |
608 |
|
*/ |
609 |
|
public void testGetWaitingThreadsIAE() { |
610 |
< |
final Mutex sync = new Mutex(); |
610 |
> |
final Mutex sync = new Mutex(); |
611 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
612 |
< |
final Mutex sync2 = new Mutex(); |
612 |
> |
final Mutex sync2 = new Mutex(); |
613 |
|
try { |
614 |
|
sync2.getWaitingThreads(c); |
615 |
|
shouldThrow(); |
620 |
|
* getWaitingThreads throws IMSE if not synced |
621 |
|
*/ |
622 |
|
public void testGetWaitingThreadsIMSE() { |
623 |
< |
final Mutex sync = new Mutex(); |
623 |
> |
final Mutex sync = new Mutex(); |
624 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
625 |
|
try { |
626 |
|
sync.getWaitingThreads(c); |
634 |
|
* hasWaiters returns true when a thread is waiting, else false |
635 |
|
*/ |
636 |
|
public void testHasWaiters() throws InterruptedException { |
637 |
< |
final Mutex sync = new Mutex(); |
637 |
> |
final Mutex sync = new Mutex(); |
638 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
639 |
< |
Thread t = new Thread(new CheckedRunnable() { |
639 |
> |
Thread t = new Thread(new CheckedRunnable() { |
640 |
|
public void realRun() throws InterruptedException { |
641 |
|
sync.acquire(1); |
642 |
|
threadAssertFalse(sync.hasWaiters(c)); |
665 |
|
* getWaitQueueLength returns number of waiting threads |
666 |
|
*/ |
667 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
668 |
< |
final Mutex sync = new Mutex(); |
668 |
> |
final Mutex sync = new Mutex(); |
669 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
670 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
670 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
671 |
|
public void realRun() throws InterruptedException { |
672 |
|
sync.acquire(1); |
673 |
|
threadAssertFalse(sync.hasWaiters(c)); |
676 |
|
sync.release(1); |
677 |
|
}}); |
678 |
|
|
679 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
679 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
680 |
|
public void realRun() throws InterruptedException { |
681 |
|
sync.acquire(1); |
682 |
|
threadAssertTrue(sync.hasWaiters(c)); |
709 |
|
* getWaitingThreads returns only and all waiting threads |
710 |
|
*/ |
711 |
|
public void testGetWaitingThreads() throws InterruptedException { |
712 |
< |
final Mutex sync = new Mutex(); |
712 |
> |
final Mutex sync = new Mutex(); |
713 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
714 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
714 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
715 |
|
public void realRun() throws InterruptedException { |
716 |
|
sync.acquire(1); |
717 |
|
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
719 |
|
sync.release(1); |
720 |
|
}}); |
721 |
|
|
722 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
722 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
723 |
|
public void realRun() throws InterruptedException { |
724 |
|
sync.acquire(1); |
725 |
|
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
757 |
|
* awaitUninterruptibly doesn't abort on interrupt |
758 |
|
*/ |
759 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
760 |
< |
final Mutex sync = new Mutex(); |
760 |
> |
final Mutex sync = new Mutex(); |
761 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
762 |
< |
Thread t = new Thread(new CheckedRunnable() { |
762 |
> |
Thread t = new Thread(new CheckedRunnable() { |
763 |
|
public void realRun() { |
764 |
|
sync.acquire(1); |
765 |
|
c.awaitUninterruptibly(); |
780 |
|
* await is interruptible |
781 |
|
*/ |
782 |
|
public void testAwait_Interrupt() throws InterruptedException { |
783 |
< |
final Mutex sync = new Mutex(); |
783 |
> |
final Mutex sync = new Mutex(); |
784 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
785 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
786 |
< |
public void realRun() throws InterruptedException { |
785 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
786 |
> |
public void realRun() throws InterruptedException { |
787 |
|
sync.acquire(1); |
788 |
|
c.await(); |
789 |
|
sync.release(1); |
800 |
|
* awaitNanos is interruptible |
801 |
|
*/ |
802 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
803 |
< |
final Mutex sync = new Mutex(); |
803 |
> |
final Mutex sync = new Mutex(); |
804 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
805 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
806 |
< |
public void realRun() throws InterruptedException { |
805 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
806 |
> |
public void realRun() throws InterruptedException { |
807 |
|
sync.acquire(1); |
808 |
|
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
809 |
|
sync.release(1); |
820 |
|
* awaitUntil is interruptible |
821 |
|
*/ |
822 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
823 |
< |
final Mutex sync = new Mutex(); |
823 |
> |
final Mutex sync = new Mutex(); |
824 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
825 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
826 |
< |
public void realRun() throws InterruptedException { |
825 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
826 |
> |
public void realRun() throws InterruptedException { |
827 |
|
sync.acquire(1); |
828 |
|
java.util.Date d = new java.util.Date(); |
829 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
841 |
|
* signalAll wakes up all threads |
842 |
|
*/ |
843 |
|
public void testSignalAll() throws InterruptedException { |
844 |
< |
final Mutex sync = new Mutex(); |
844 |
> |
final Mutex sync = new Mutex(); |
845 |
|
final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); |
846 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
846 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
847 |
|
public void realRun() throws InterruptedException { |
848 |
|
sync.acquire(1); |
849 |
|
c.await(); |
850 |
|
sync.release(1); |
851 |
|
}}); |
852 |
|
|
853 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
853 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
854 |
|
public void realRun() throws InterruptedException { |
855 |
|
sync.acquire(1); |
856 |
|
c.await(); |
906 |
|
* tryReleaseShared setting state changes getState |
907 |
|
*/ |
908 |
|
public void testGetStateWithReleaseShared() { |
909 |
< |
final BooleanLatch l = new BooleanLatch(); |
910 |
< |
assertFalse(l.isSignalled()); |
911 |
< |
l.releaseShared(0); |
912 |
< |
assertTrue(l.isSignalled()); |
909 |
> |
final BooleanLatch l = new BooleanLatch(); |
910 |
> |
assertFalse(l.isSignalled()); |
911 |
> |
l.releaseShared(0); |
912 |
> |
assertTrue(l.isSignalled()); |
913 |
|
} |
914 |
|
|
915 |
|
/** |
916 |
|
* releaseShared has no effect when already signalled |
917 |
|
*/ |
918 |
|
public void testReleaseShared() { |
919 |
< |
final BooleanLatch l = new BooleanLatch(); |
920 |
< |
assertFalse(l.isSignalled()); |
921 |
< |
l.releaseShared(0); |
922 |
< |
assertTrue(l.isSignalled()); |
923 |
< |
l.releaseShared(0); |
924 |
< |
assertTrue(l.isSignalled()); |
919 |
> |
final BooleanLatch l = new BooleanLatch(); |
920 |
> |
assertFalse(l.isSignalled()); |
921 |
> |
l.releaseShared(0); |
922 |
> |
assertTrue(l.isSignalled()); |
923 |
> |
l.releaseShared(0); |
924 |
> |
assertTrue(l.isSignalled()); |
925 |
|
} |
926 |
|
|
927 |
|
/** |
928 |
|
* acquireSharedInterruptibly returns after release, but not before |
929 |
|
*/ |
930 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
931 |
< |
final BooleanLatch l = new BooleanLatch(); |
931 |
> |
final BooleanLatch l = new BooleanLatch(); |
932 |
|
|
933 |
< |
Thread t = new Thread(new CheckedRunnable() { |
933 |
> |
Thread t = new Thread(new CheckedRunnable() { |
934 |
|
public void realRun() throws InterruptedException { |
935 |
|
threadAssertFalse(l.isSignalled()); |
936 |
|
l.acquireSharedInterruptibly(0); |
950 |
|
* acquireSharedTimed returns after release |
951 |
|
*/ |
952 |
|
public void testAsquireSharedTimed() throws InterruptedException { |
953 |
< |
final BooleanLatch l = new BooleanLatch(); |
953 |
> |
final BooleanLatch l = new BooleanLatch(); |
954 |
|
|
955 |
< |
Thread t = new Thread(new CheckedRunnable() { |
955 |
> |
Thread t = new Thread(new CheckedRunnable() { |
956 |
|
public void realRun() throws InterruptedException { |
957 |
|
threadAssertFalse(l.isSignalled()); |
958 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
972 |
|
*/ |
973 |
|
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
974 |
|
final BooleanLatch l = new BooleanLatch(); |
975 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
976 |
< |
public void realRun() throws InterruptedException { |
975 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
976 |
> |
public void realRun() throws InterruptedException { |
977 |
|
threadAssertFalse(l.isSignalled()); |
978 |
|
l.acquireSharedInterruptibly(0); |
979 |
|
}}); |
980 |
|
|
981 |
< |
t.start(); |
981 |
> |
t.start(); |
982 |
|
assertFalse(l.isSignalled()); |
983 |
|
t.interrupt(); |
984 |
|
t.join(); |
989 |
|
*/ |
990 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
991 |
|
final BooleanLatch l = new BooleanLatch(); |
992 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
993 |
< |
public void realRun() throws InterruptedException { |
992 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
993 |
> |
public void realRun() throws InterruptedException { |
994 |
|
threadAssertFalse(l.isSignalled()); |
995 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
996 |
|
}}); |
1007 |
|
*/ |
1008 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1009 |
|
final BooleanLatch l = new BooleanLatch(); |
1010 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1010 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1011 |
|
public void realRun() throws InterruptedException { |
1012 |
|
threadAssertFalse(l.isSignalled()); |
1013 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |