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 |
|
|
97 |
|
* isHeldExclusively is false upon construction |
98 |
|
*/ |
99 |
|
public void testIsHeldExclusively() { |
100 |
< |
Mutex rl = new Mutex(); |
100 |
> |
Mutex rl = new Mutex(); |
101 |
|
assertFalse(rl.isHeldExclusively()); |
102 |
|
} |
103 |
|
|
105 |
|
* acquiring released sync succeeds |
106 |
|
*/ |
107 |
|
public void testAcquire() { |
108 |
< |
Mutex rl = new Mutex(); |
108 |
> |
Mutex rl = new Mutex(); |
109 |
|
rl.acquire(1); |
110 |
|
assertTrue(rl.isHeldExclusively()); |
111 |
|
rl.release(1); |
116 |
|
* tryAcquire on an released sync succeeds |
117 |
|
*/ |
118 |
|
public void testTryAcquire() { |
119 |
< |
Mutex rl = new Mutex(); |
119 |
> |
Mutex rl = new Mutex(); |
120 |
|
assertTrue(rl.tryAcquire(1)); |
121 |
|
assertTrue(rl.isHeldExclusively()); |
122 |
|
rl.release(1); |
126 |
|
* hasQueuedThreads reports whether there are waiting threads |
127 |
|
*/ |
128 |
|
public void testhasQueuedThreads() throws InterruptedException { |
129 |
< |
final Mutex sync = new Mutex(); |
129 |
> |
final Mutex sync = new Mutex(); |
130 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
131 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
132 |
|
assertFalse(sync.hasQueuedThreads()); |
151 |
|
* isQueued(null) throws NPE |
152 |
|
*/ |
153 |
|
public void testIsQueuedNPE() { |
154 |
< |
final Mutex sync = new Mutex(); |
154 |
> |
final Mutex sync = new Mutex(); |
155 |
|
try { |
156 |
|
sync.isQueued(null); |
157 |
|
shouldThrow(); |
162 |
|
* isQueued reports whether a thread is queued. |
163 |
|
*/ |
164 |
|
public void testIsQueued() throws InterruptedException { |
165 |
< |
final Mutex sync = new Mutex(); |
165 |
> |
final Mutex sync = new Mutex(); |
166 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
167 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
168 |
|
assertFalse(sync.isQueued(t1)); |
192 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
193 |
|
*/ |
194 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
195 |
< |
final Mutex sync = new Mutex(); |
195 |
> |
final Mutex sync = new Mutex(); |
196 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
197 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
198 |
|
assertNull(sync.getFirstQueuedThread()); |
219 |
|
* hasContended reports false if no thread has ever blocked, else true |
220 |
|
*/ |
221 |
|
public void testHasContended() throws InterruptedException { |
222 |
< |
final Mutex sync = new Mutex(); |
222 |
> |
final Mutex sync = new Mutex(); |
223 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
224 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
225 |
|
assertFalse(sync.hasContended()); |
244 |
|
* getQueuedThreads includes waiting threads |
245 |
|
*/ |
246 |
|
public void testGetQueuedThreads() throws InterruptedException { |
247 |
< |
final Mutex sync = new Mutex(); |
247 |
> |
final Mutex sync = new Mutex(); |
248 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
249 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
250 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
272 |
|
* getExclusiveQueuedThreads includes waiting threads |
273 |
|
*/ |
274 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
275 |
< |
final Mutex sync = new Mutex(); |
275 |
> |
final Mutex sync = new Mutex(); |
276 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
277 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
278 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
300 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
301 |
|
*/ |
302 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
303 |
< |
final Mutex sync = new Mutex(); |
303 |
> |
final Mutex sync = new Mutex(); |
304 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
305 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
306 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
326 |
|
* tryAcquireNanos is interruptible. |
327 |
|
*/ |
328 |
|
public void testInterruptedException2() throws InterruptedException { |
329 |
< |
final Mutex sync = new Mutex(); |
330 |
< |
sync.acquire(1); |
331 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
329 |
> |
final Mutex sync = new Mutex(); |
330 |
> |
sync.acquire(1); |
331 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
332 |
|
public void realRun() throws InterruptedException { |
333 |
|
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
334 |
|
}}); |
343 |
|
* TryAcquire on exclusively held sync fails |
344 |
|
*/ |
345 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
346 |
< |
final Mutex sync = new Mutex(); |
347 |
< |
sync.acquire(1); |
348 |
< |
Thread t = new Thread(new CheckedRunnable() { |
346 |
> |
final Mutex sync = new Mutex(); |
347 |
> |
sync.acquire(1); |
348 |
> |
Thread t = new Thread(new CheckedRunnable() { |
349 |
|
public void realRun() { |
350 |
|
threadAssertFalse(sync.tryAcquire(1)); |
351 |
|
}}); |
359 |
|
* tryAcquireNanos on an exclusively held sync times out |
360 |
|
*/ |
361 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
362 |
< |
final Mutex sync = new Mutex(); |
363 |
< |
sync.acquire(1); |
364 |
< |
Thread t = new Thread(new CheckedRunnable() { |
362 |
> |
final Mutex sync = new Mutex(); |
363 |
> |
sync.acquire(1); |
364 |
> |
Thread t = new Thread(new CheckedRunnable() { |
365 |
|
public void realRun() throws InterruptedException { |
366 |
|
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
367 |
|
}}); |
376 |
|
* getState is true when acquired and false when not |
377 |
|
*/ |
378 |
|
public void testGetState() throws InterruptedException { |
379 |
< |
final Mutex sync = new Mutex(); |
380 |
< |
sync.acquire(1); |
381 |
< |
assertTrue(sync.isHeldExclusively()); |
382 |
< |
sync.release(1); |
383 |
< |
assertFalse(sync.isHeldExclusively()); |
384 |
< |
Thread t = new Thread(new CheckedRunnable() { |
379 |
> |
final Mutex sync = new Mutex(); |
380 |
> |
sync.acquire(1); |
381 |
> |
assertTrue(sync.isHeldExclusively()); |
382 |
> |
sync.release(1); |
383 |
> |
assertFalse(sync.isHeldExclusively()); |
384 |
> |
Thread t = new Thread(new CheckedRunnable() { |
385 |
|
public void realRun() throws InterruptedException { |
386 |
|
sync.acquire(1); |
387 |
|
Thread.sleep(SMALL_DELAY_MS); |
400 |
|
* acquireInterruptibly is interruptible. |
401 |
|
*/ |
402 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
403 |
< |
final Mutex sync = new Mutex(); |
404 |
< |
sync.acquire(1); |
405 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
403 |
> |
final Mutex sync = new Mutex(); |
404 |
> |
sync.acquire(1); |
405 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
406 |
|
t.start(); |
407 |
|
Thread.sleep(SHORT_DELAY_MS); |
408 |
|
t.interrupt(); |
415 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
416 |
|
*/ |
417 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
418 |
< |
final Mutex sync = new Mutex(); |
418 |
> |
final Mutex sync = new Mutex(); |
419 |
|
sync.acquireInterruptibly(1); |
420 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
420 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
421 |
|
t.start(); |
422 |
|
t.interrupt(); |
423 |
|
assertTrue(sync.isHeldExclusively()); |
428 |
|
* owns is true for a condition created by sync else false |
429 |
|
*/ |
430 |
|
public void testOwns() { |
431 |
< |
final Mutex sync = new Mutex(); |
431 |
> |
final Mutex sync = new Mutex(); |
432 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
433 |
|
final Mutex sync2 = new Mutex(); |
434 |
|
assertTrue(sync.owns(c)); |
439 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
440 |
|
*/ |
441 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
442 |
< |
final Mutex sync = new Mutex(); |
442 |
> |
final Mutex sync = new Mutex(); |
443 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
444 |
|
try { |
445 |
|
c.await(); |
451 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
452 |
|
*/ |
453 |
|
public void testSignal_IllegalMonitor() throws InterruptedException { |
454 |
< |
final Mutex sync = new Mutex(); |
454 |
> |
final Mutex sync = new Mutex(); |
455 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
456 |
|
try { |
457 |
|
c.signal(); |
463 |
|
* awaitNanos without a signal times out |
464 |
|
*/ |
465 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
466 |
< |
final Mutex sync = new Mutex(); |
466 |
> |
final Mutex sync = new Mutex(); |
467 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
468 |
|
sync.acquire(1); |
469 |
|
long t = c.awaitNanos(100); |
475 |
|
* Timed await without a signal times out |
476 |
|
*/ |
477 |
|
public void testAwait_Timeout() throws InterruptedException { |
478 |
< |
final Mutex sync = new Mutex(); |
478 |
> |
final Mutex sync = new Mutex(); |
479 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
480 |
|
sync.acquire(1); |
481 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
481 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
482 |
|
sync.release(1); |
483 |
|
} |
484 |
|
|
486 |
|
* awaitUntil without a signal times out |
487 |
|
*/ |
488 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
489 |
< |
final Mutex sync = new Mutex(); |
489 |
> |
final Mutex sync = new Mutex(); |
490 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
491 |
|
sync.acquire(1); |
492 |
|
java.util.Date d = new java.util.Date(); |
498 |
|
* await returns when signalled |
499 |
|
*/ |
500 |
|
public void testAwait() throws InterruptedException { |
501 |
< |
final Mutex sync = new Mutex(); |
501 |
> |
final Mutex sync = new Mutex(); |
502 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
503 |
< |
Thread t = new Thread(new CheckedRunnable() { |
503 |
> |
Thread t = new Thread(new CheckedRunnable() { |
504 |
|
public void realRun() throws InterruptedException { |
505 |
|
sync.acquire(1); |
506 |
|
c.await(); |
522 |
|
* hasWaiters throws NPE if null |
523 |
|
*/ |
524 |
|
public void testHasWaitersNPE() { |
525 |
< |
final Mutex sync = new Mutex(); |
525 |
> |
final Mutex sync = new Mutex(); |
526 |
|
try { |
527 |
|
sync.hasWaiters(null); |
528 |
|
shouldThrow(); |
533 |
|
* getWaitQueueLength throws NPE if null |
534 |
|
*/ |
535 |
|
public void testGetWaitQueueLengthNPE() { |
536 |
< |
final Mutex sync = new Mutex(); |
536 |
> |
final Mutex sync = new Mutex(); |
537 |
|
try { |
538 |
|
sync.getWaitQueueLength(null); |
539 |
|
shouldThrow(); |
545 |
|
* getWaitingThreads throws NPE if null |
546 |
|
*/ |
547 |
|
public void testGetWaitingThreadsNPE() { |
548 |
< |
final Mutex sync = new Mutex(); |
548 |
> |
final Mutex sync = new Mutex(); |
549 |
|
try { |
550 |
|
sync.getWaitingThreads(null); |
551 |
|
shouldThrow(); |
557 |
|
* hasWaiters throws IAE if not owned |
558 |
|
*/ |
559 |
|
public void testHasWaitersIAE() { |
560 |
< |
final Mutex sync = new Mutex(); |
560 |
> |
final Mutex sync = new Mutex(); |
561 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
562 |
< |
final Mutex sync2 = new Mutex(); |
562 |
> |
final Mutex sync2 = new Mutex(); |
563 |
|
try { |
564 |
|
sync2.hasWaiters(c); |
565 |
|
shouldThrow(); |
570 |
|
* hasWaiters throws IMSE if not synced |
571 |
|
*/ |
572 |
|
public void testHasWaitersIMSE() { |
573 |
< |
final Mutex sync = new Mutex(); |
573 |
> |
final Mutex sync = new Mutex(); |
574 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
575 |
|
try { |
576 |
|
sync.hasWaiters(c); |
583 |
|
* getWaitQueueLength throws IAE if not owned |
584 |
|
*/ |
585 |
|
public void testGetWaitQueueLengthIAE() { |
586 |
< |
final Mutex sync = new Mutex(); |
586 |
> |
final Mutex sync = new Mutex(); |
587 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
588 |
< |
final Mutex sync2 = new Mutex(); |
588 |
> |
final Mutex sync2 = new Mutex(); |
589 |
|
try { |
590 |
|
sync2.getWaitQueueLength(c); |
591 |
|
shouldThrow(); |
596 |
|
* getWaitQueueLength throws IMSE if not synced |
597 |
|
*/ |
598 |
|
public void testGetWaitQueueLengthIMSE() { |
599 |
< |
final Mutex sync = new Mutex(); |
599 |
> |
final Mutex sync = new Mutex(); |
600 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
601 |
|
try { |
602 |
|
sync.getWaitQueueLength(c); |
609 |
|
* getWaitingThreads throws IAE if not owned |
610 |
|
*/ |
611 |
|
public void testGetWaitingThreadsIAE() { |
612 |
< |
final Mutex sync = new Mutex(); |
612 |
> |
final Mutex sync = new Mutex(); |
613 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
614 |
< |
final Mutex sync2 = new Mutex(); |
614 |
> |
final Mutex sync2 = new Mutex(); |
615 |
|
try { |
616 |
|
sync2.getWaitingThreads(c); |
617 |
|
shouldThrow(); |
622 |
|
* getWaitingThreads throws IMSE if not synced |
623 |
|
*/ |
624 |
|
public void testGetWaitingThreadsIMSE() { |
625 |
< |
final Mutex sync = new Mutex(); |
625 |
> |
final Mutex sync = new Mutex(); |
626 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
627 |
|
try { |
628 |
|
sync.getWaitingThreads(c); |
636 |
|
* hasWaiters returns true when a thread is waiting, else false |
637 |
|
*/ |
638 |
|
public void testHasWaiters() throws InterruptedException { |
639 |
< |
final Mutex sync = new Mutex(); |
639 |
> |
final Mutex sync = new Mutex(); |
640 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
641 |
< |
Thread t = new Thread(new CheckedRunnable() { |
641 |
> |
Thread t = new Thread(new CheckedRunnable() { |
642 |
|
public void realRun() throws InterruptedException { |
643 |
|
sync.acquire(1); |
644 |
|
threadAssertFalse(sync.hasWaiters(c)); |
667 |
|
* getWaitQueueLength returns number of waiting threads |
668 |
|
*/ |
669 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
670 |
< |
final Mutex sync = new Mutex(); |
670 |
> |
final Mutex sync = new Mutex(); |
671 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
672 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
672 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
673 |
|
public void realRun() throws InterruptedException { |
674 |
|
sync.acquire(1); |
675 |
|
threadAssertFalse(sync.hasWaiters(c)); |
678 |
|
sync.release(1); |
679 |
|
}}); |
680 |
|
|
681 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
681 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
682 |
|
public void realRun() throws InterruptedException { |
683 |
|
sync.acquire(1); |
684 |
|
threadAssertTrue(sync.hasWaiters(c)); |
711 |
|
* getWaitingThreads returns only and all waiting threads |
712 |
|
*/ |
713 |
|
public void testGetWaitingThreads() throws InterruptedException { |
714 |
< |
final Mutex sync = new Mutex(); |
714 |
> |
final Mutex sync = new Mutex(); |
715 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
716 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
716 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
717 |
|
public void realRun() throws InterruptedException { |
718 |
|
sync.acquire(1); |
719 |
|
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
721 |
|
sync.release(1); |
722 |
|
}}); |
723 |
|
|
724 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
724 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
725 |
|
public void realRun() throws InterruptedException { |
726 |
|
sync.acquire(1); |
727 |
|
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
759 |
|
* awaitUninterruptibly doesn't abort on interrupt |
760 |
|
*/ |
761 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
762 |
< |
final Mutex sync = new Mutex(); |
762 |
> |
final Mutex sync = new Mutex(); |
763 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
764 |
< |
Thread t = new Thread(new CheckedRunnable() { |
764 |
> |
Thread t = new Thread(new CheckedRunnable() { |
765 |
|
public void realRun() { |
766 |
|
sync.acquire(1); |
767 |
|
c.awaitUninterruptibly(); |
782 |
|
* await is interruptible |
783 |
|
*/ |
784 |
|
public void testAwait_Interrupt() throws InterruptedException { |
785 |
< |
final Mutex sync = new Mutex(); |
785 |
> |
final Mutex sync = new Mutex(); |
786 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
787 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
788 |
< |
public void realRun() throws InterruptedException { |
787 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
788 |
> |
public void realRun() throws InterruptedException { |
789 |
|
sync.acquire(1); |
790 |
|
c.await(); |
791 |
|
sync.release(1); |
802 |
|
* awaitNanos is interruptible |
803 |
|
*/ |
804 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
805 |
< |
final Mutex sync = new Mutex(); |
805 |
> |
final Mutex sync = new Mutex(); |
806 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
807 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
808 |
< |
public void realRun() throws InterruptedException { |
807 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
808 |
> |
public void realRun() throws InterruptedException { |
809 |
|
sync.acquire(1); |
810 |
|
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
811 |
|
sync.release(1); |
822 |
|
* awaitUntil is interruptible |
823 |
|
*/ |
824 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
825 |
< |
final Mutex sync = new Mutex(); |
825 |
> |
final Mutex sync = new Mutex(); |
826 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
827 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
828 |
< |
public void realRun() throws InterruptedException { |
827 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
828 |
> |
public void realRun() throws InterruptedException { |
829 |
|
sync.acquire(1); |
830 |
|
java.util.Date d = new java.util.Date(); |
831 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
843 |
|
* signalAll wakes up all threads |
844 |
|
*/ |
845 |
|
public void testSignalAll() throws InterruptedException { |
846 |
< |
final Mutex sync = new Mutex(); |
846 |
> |
final Mutex sync = new Mutex(); |
847 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
848 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
848 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
849 |
|
public void realRun() throws InterruptedException { |
850 |
|
sync.acquire(1); |
851 |
|
c.await(); |
852 |
|
sync.release(1); |
853 |
|
}}); |
854 |
|
|
855 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
855 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
856 |
|
public void realRun() throws InterruptedException { |
857 |
|
sync.acquire(1); |
858 |
|
c.await(); |
908 |
|
* tryReleaseShared setting state changes getState |
909 |
|
*/ |
910 |
|
public void testGetStateWithReleaseShared() { |
911 |
< |
final BooleanLatch l = new BooleanLatch(); |
912 |
< |
assertFalse(l.isSignalled()); |
913 |
< |
l.releaseShared(0); |
914 |
< |
assertTrue(l.isSignalled()); |
911 |
> |
final BooleanLatch l = new BooleanLatch(); |
912 |
> |
assertFalse(l.isSignalled()); |
913 |
> |
l.releaseShared(0); |
914 |
> |
assertTrue(l.isSignalled()); |
915 |
|
} |
916 |
|
|
917 |
|
/** |
918 |
|
* releaseShared has no effect when already signalled |
919 |
|
*/ |
920 |
|
public void testReleaseShared() { |
921 |
< |
final BooleanLatch l = new BooleanLatch(); |
922 |
< |
assertFalse(l.isSignalled()); |
923 |
< |
l.releaseShared(0); |
924 |
< |
assertTrue(l.isSignalled()); |
925 |
< |
l.releaseShared(0); |
926 |
< |
assertTrue(l.isSignalled()); |
921 |
> |
final BooleanLatch l = new BooleanLatch(); |
922 |
> |
assertFalse(l.isSignalled()); |
923 |
> |
l.releaseShared(0); |
924 |
> |
assertTrue(l.isSignalled()); |
925 |
> |
l.releaseShared(0); |
926 |
> |
assertTrue(l.isSignalled()); |
927 |
|
} |
928 |
|
|
929 |
|
/** |
930 |
|
* acquireSharedInterruptibly returns after release, but not before |
931 |
|
*/ |
932 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
933 |
< |
final BooleanLatch l = new BooleanLatch(); |
933 |
> |
final BooleanLatch l = new BooleanLatch(); |
934 |
|
|
935 |
< |
Thread t = new Thread(new CheckedRunnable() { |
935 |
> |
Thread t = new Thread(new CheckedRunnable() { |
936 |
|
public void realRun() throws InterruptedException { |
937 |
|
threadAssertFalse(l.isSignalled()); |
938 |
|
l.acquireSharedInterruptibly(0); |
952 |
|
* acquireSharedTimed returns after release |
953 |
|
*/ |
954 |
|
public void testAsquireSharedTimed() throws InterruptedException { |
955 |
< |
final BooleanLatch l = new BooleanLatch(); |
955 |
> |
final BooleanLatch l = new BooleanLatch(); |
956 |
|
|
957 |
< |
Thread t = new Thread(new CheckedRunnable() { |
957 |
> |
Thread t = new Thread(new CheckedRunnable() { |
958 |
|
public void realRun() throws InterruptedException { |
959 |
|
threadAssertFalse(l.isSignalled()); |
960 |
|
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
974 |
|
*/ |
975 |
|
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
976 |
|
final BooleanLatch l = new BooleanLatch(); |
977 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
978 |
< |
public void realRun() throws InterruptedException { |
977 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
978 |
> |
public void realRun() throws InterruptedException { |
979 |
|
threadAssertFalse(l.isSignalled()); |
980 |
|
l.acquireSharedInterruptibly(0); |
981 |
|
}}); |
982 |
|
|
983 |
< |
t.start(); |
983 |
> |
t.start(); |
984 |
|
assertFalse(l.isSignalled()); |
985 |
|
t.interrupt(); |
986 |
|
t.join(); |
991 |
|
*/ |
992 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
993 |
|
final BooleanLatch l = new BooleanLatch(); |
994 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
995 |
< |
public void realRun() throws InterruptedException { |
994 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
995 |
> |
public void realRun() throws InterruptedException { |
996 |
|
threadAssertFalse(l.isSignalled()); |
997 |
|
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
998 |
|
}}); |
1009 |
|
*/ |
1010 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1011 |
|
final BooleanLatch l = new BooleanLatch(); |
1012 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1012 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1013 |
|
public void realRun() throws InterruptedException { |
1014 |
|
threadAssertFalse(l.isSignalled()); |
1015 |
|
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |