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); |
333 |
> |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000L * 1000L); |
334 |
|
}}); |
335 |
|
|
336 |
|
t.start(); |
337 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
338 |
|
t.interrupt(); |
339 |
|
t.join(); |
340 |
|
} |
344 |
|
* TryAcquire on exclusively held sync fails |
345 |
|
*/ |
346 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
347 |
< |
final Mutex sync = new Mutex(); |
348 |
< |
sync.acquire(1); |
349 |
< |
Thread t = new Thread(new CheckedRunnable() { |
347 |
> |
final Mutex sync = new Mutex(); |
348 |
> |
sync.acquire(1); |
349 |
> |
Thread t = new Thread(new CheckedRunnable() { |
350 |
|
public void realRun() { |
351 |
|
threadAssertFalse(sync.tryAcquire(1)); |
352 |
|
}}); |
360 |
|
* tryAcquireNanos on an exclusively held sync times out |
361 |
|
*/ |
362 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
363 |
< |
final Mutex sync = new Mutex(); |
364 |
< |
sync.acquire(1); |
365 |
< |
Thread t = new Thread(new CheckedRunnable() { |
363 |
> |
final Mutex sync = new Mutex(); |
364 |
> |
sync.acquire(1); |
365 |
> |
Thread t = new Thread(new CheckedRunnable() { |
366 |
|
public void realRun() throws InterruptedException { |
367 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
367 |
> |
threadAssertFalse(sync.tryAcquireNanos(1, SHORT_DELAY_MS * 1000L * 1000L)); |
368 |
|
}}); |
369 |
|
|
370 |
|
t.start(); |
377 |
|
* getState is true when acquired and false when not |
378 |
|
*/ |
379 |
|
public void testGetState() throws InterruptedException { |
380 |
< |
final Mutex sync = new Mutex(); |
381 |
< |
sync.acquire(1); |
382 |
< |
assertTrue(sync.isHeldExclusively()); |
383 |
< |
sync.release(1); |
384 |
< |
assertFalse(sync.isHeldExclusively()); |
385 |
< |
Thread t = new Thread(new CheckedRunnable() { |
380 |
> |
final Mutex sync = new Mutex(); |
381 |
> |
sync.acquire(1); |
382 |
> |
assertTrue(sync.isHeldExclusively()); |
383 |
> |
sync.release(1); |
384 |
> |
assertFalse(sync.isHeldExclusively()); |
385 |
> |
Thread t = new Thread(new CheckedRunnable() { |
386 |
|
public void realRun() throws InterruptedException { |
387 |
|
sync.acquire(1); |
388 |
|
Thread.sleep(SMALL_DELAY_MS); |
401 |
|
* acquireInterruptibly is interruptible. |
402 |
|
*/ |
403 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
404 |
< |
final Mutex sync = new Mutex(); |
405 |
< |
sync.acquire(1); |
406 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
404 |
> |
final Mutex sync = new Mutex(); |
405 |
> |
sync.acquire(1); |
406 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
407 |
|
t.start(); |
408 |
|
Thread.sleep(SHORT_DELAY_MS); |
409 |
|
t.interrupt(); |
416 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
417 |
|
*/ |
418 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
419 |
< |
final Mutex sync = new Mutex(); |
419 |
> |
final Mutex sync = new Mutex(); |
420 |
|
sync.acquireInterruptibly(1); |
421 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
421 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
422 |
|
t.start(); |
423 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
424 |
|
t.interrupt(); |
425 |
|
assertTrue(sync.isHeldExclusively()); |
426 |
|
t.join(); |
430 |
|
* owns is true for a condition created by sync else false |
431 |
|
*/ |
432 |
|
public void testOwns() { |
433 |
< |
final Mutex sync = new Mutex(); |
433 |
> |
final Mutex sync = new Mutex(); |
434 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
435 |
|
final Mutex sync2 = new Mutex(); |
436 |
|
assertTrue(sync.owns(c)); |
441 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
442 |
|
*/ |
443 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
444 |
< |
final Mutex sync = new Mutex(); |
444 |
> |
final Mutex sync = new Mutex(); |
445 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
446 |
|
try { |
447 |
|
c.await(); |
453 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
454 |
|
*/ |
455 |
|
public void testSignal_IllegalMonitor() throws InterruptedException { |
456 |
< |
final Mutex sync = new Mutex(); |
456 |
> |
final Mutex sync = new Mutex(); |
457 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
458 |
|
try { |
459 |
|
c.signal(); |
465 |
|
* awaitNanos without a signal times out |
466 |
|
*/ |
467 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
468 |
< |
final Mutex sync = new Mutex(); |
468 |
> |
final Mutex sync = new Mutex(); |
469 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
470 |
|
sync.acquire(1); |
471 |
|
long t = c.awaitNanos(100); |
477 |
|
* Timed await without a signal times out |
478 |
|
*/ |
479 |
|
public void testAwait_Timeout() throws InterruptedException { |
480 |
< |
final Mutex sync = new Mutex(); |
480 |
> |
final Mutex sync = new Mutex(); |
481 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
482 |
|
sync.acquire(1); |
483 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
483 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
484 |
|
sync.release(1); |
485 |
|
} |
486 |
|
|
488 |
|
* awaitUntil without a signal times out |
489 |
|
*/ |
490 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
491 |
< |
final Mutex sync = new Mutex(); |
491 |
> |
final Mutex sync = new Mutex(); |
492 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
493 |
|
sync.acquire(1); |
494 |
|
java.util.Date d = new java.util.Date(); |
500 |
|
* await returns when signalled |
501 |
|
*/ |
502 |
|
public void testAwait() throws InterruptedException { |
503 |
< |
final Mutex sync = new Mutex(); |
503 |
> |
final Mutex sync = new Mutex(); |
504 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
505 |
< |
Thread t = new Thread(new CheckedRunnable() { |
505 |
> |
Thread t = new Thread(new CheckedRunnable() { |
506 |
|
public void realRun() throws InterruptedException { |
507 |
|
sync.acquire(1); |
508 |
|
c.await(); |
524 |
|
* hasWaiters throws NPE if null |
525 |
|
*/ |
526 |
|
public void testHasWaitersNPE() { |
527 |
< |
final Mutex sync = new Mutex(); |
527 |
> |
final Mutex sync = new Mutex(); |
528 |
|
try { |
529 |
|
sync.hasWaiters(null); |
530 |
|
shouldThrow(); |
535 |
|
* getWaitQueueLength throws NPE if null |
536 |
|
*/ |
537 |
|
public void testGetWaitQueueLengthNPE() { |
538 |
< |
final Mutex sync = new Mutex(); |
538 |
> |
final Mutex sync = new Mutex(); |
539 |
|
try { |
540 |
|
sync.getWaitQueueLength(null); |
541 |
|
shouldThrow(); |
547 |
|
* getWaitingThreads throws NPE if null |
548 |
|
*/ |
549 |
|
public void testGetWaitingThreadsNPE() { |
550 |
< |
final Mutex sync = new Mutex(); |
550 |
> |
final Mutex sync = new Mutex(); |
551 |
|
try { |
552 |
|
sync.getWaitingThreads(null); |
553 |
|
shouldThrow(); |
559 |
|
* hasWaiters throws IAE if not owned |
560 |
|
*/ |
561 |
|
public void testHasWaitersIAE() { |
562 |
< |
final Mutex sync = new Mutex(); |
562 |
> |
final Mutex sync = new Mutex(); |
563 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
564 |
< |
final Mutex sync2 = new Mutex(); |
564 |
> |
final Mutex sync2 = new Mutex(); |
565 |
|
try { |
566 |
|
sync2.hasWaiters(c); |
567 |
|
shouldThrow(); |
572 |
|
* hasWaiters throws IMSE if not synced |
573 |
|
*/ |
574 |
|
public void testHasWaitersIMSE() { |
575 |
< |
final Mutex sync = new Mutex(); |
575 |
> |
final Mutex sync = new Mutex(); |
576 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
577 |
|
try { |
578 |
|
sync.hasWaiters(c); |
585 |
|
* getWaitQueueLength throws IAE if not owned |
586 |
|
*/ |
587 |
|
public void testGetWaitQueueLengthIAE() { |
588 |
< |
final Mutex sync = new Mutex(); |
588 |
> |
final Mutex sync = new Mutex(); |
589 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
590 |
< |
final Mutex sync2 = new Mutex(); |
590 |
> |
final Mutex sync2 = new Mutex(); |
591 |
|
try { |
592 |
|
sync2.getWaitQueueLength(c); |
593 |
|
shouldThrow(); |
598 |
|
* getWaitQueueLength throws IMSE if not synced |
599 |
|
*/ |
600 |
|
public void testGetWaitQueueLengthIMSE() { |
601 |
< |
final Mutex sync = new Mutex(); |
601 |
> |
final Mutex sync = new Mutex(); |
602 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
603 |
|
try { |
604 |
|
sync.getWaitQueueLength(c); |
611 |
|
* getWaitingThreads throws IAE if not owned |
612 |
|
*/ |
613 |
|
public void testGetWaitingThreadsIAE() { |
614 |
< |
final Mutex sync = new Mutex(); |
614 |
> |
final Mutex sync = new Mutex(); |
615 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
616 |
< |
final Mutex sync2 = new Mutex(); |
616 |
> |
final Mutex sync2 = new Mutex(); |
617 |
|
try { |
618 |
|
sync2.getWaitingThreads(c); |
619 |
|
shouldThrow(); |
624 |
|
* getWaitingThreads throws IMSE if not synced |
625 |
|
*/ |
626 |
|
public void testGetWaitingThreadsIMSE() { |
627 |
< |
final Mutex sync = new Mutex(); |
627 |
> |
final Mutex sync = new Mutex(); |
628 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
629 |
|
try { |
630 |
|
sync.getWaitingThreads(c); |
638 |
|
* hasWaiters returns true when a thread is waiting, else false |
639 |
|
*/ |
640 |
|
public void testHasWaiters() throws InterruptedException { |
641 |
< |
final Mutex sync = new Mutex(); |
641 |
> |
final Mutex sync = new Mutex(); |
642 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
643 |
< |
Thread t = new Thread(new CheckedRunnable() { |
643 |
> |
Thread t = new Thread(new CheckedRunnable() { |
644 |
|
public void realRun() throws InterruptedException { |
645 |
|
sync.acquire(1); |
646 |
|
threadAssertFalse(sync.hasWaiters(c)); |
669 |
|
* getWaitQueueLength returns number of waiting threads |
670 |
|
*/ |
671 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
672 |
< |
final Mutex sync = new Mutex(); |
672 |
> |
final Mutex sync = new Mutex(); |
673 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
674 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
674 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
675 |
|
public void realRun() throws InterruptedException { |
676 |
|
sync.acquire(1); |
677 |
|
threadAssertFalse(sync.hasWaiters(c)); |
680 |
|
sync.release(1); |
681 |
|
}}); |
682 |
|
|
683 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
683 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
684 |
|
public void realRun() throws InterruptedException { |
685 |
|
sync.acquire(1); |
686 |
|
threadAssertTrue(sync.hasWaiters(c)); |
713 |
|
* getWaitingThreads returns only and all waiting threads |
714 |
|
*/ |
715 |
|
public void testGetWaitingThreads() throws InterruptedException { |
716 |
< |
final Mutex sync = new Mutex(); |
716 |
> |
final Mutex sync = new Mutex(); |
717 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
718 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
718 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
719 |
|
public void realRun() throws InterruptedException { |
720 |
|
sync.acquire(1); |
721 |
|
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
723 |
|
sync.release(1); |
724 |
|
}}); |
725 |
|
|
726 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
726 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
727 |
|
public void realRun() throws InterruptedException { |
728 |
|
sync.acquire(1); |
729 |
|
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
761 |
|
* awaitUninterruptibly doesn't abort on interrupt |
762 |
|
*/ |
763 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
764 |
< |
final Mutex sync = new Mutex(); |
764 |
> |
final Mutex sync = new Mutex(); |
765 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
766 |
< |
Thread t = new Thread(new CheckedRunnable() { |
766 |
> |
Thread t = new Thread(new CheckedRunnable() { |
767 |
|
public void realRun() { |
768 |
|
sync.acquire(1); |
769 |
|
c.awaitUninterruptibly(); |
784 |
|
* await is interruptible |
785 |
|
*/ |
786 |
|
public void testAwait_Interrupt() throws InterruptedException { |
787 |
< |
final Mutex sync = new Mutex(); |
787 |
> |
final Mutex sync = new Mutex(); |
788 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
789 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
790 |
< |
public void realRun() throws InterruptedException { |
789 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
790 |
> |
public void realRun() throws InterruptedException { |
791 |
|
sync.acquire(1); |
792 |
|
c.await(); |
790 |
– |
sync.release(1); |
793 |
|
}}); |
794 |
|
|
795 |
|
t.start(); |
803 |
|
* awaitNanos is interruptible |
804 |
|
*/ |
805 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
806 |
< |
final Mutex sync = new Mutex(); |
806 |
> |
final Mutex sync = new Mutex(); |
807 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
808 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
809 |
< |
public void realRun() throws InterruptedException { |
808 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
809 |
> |
public void realRun() throws InterruptedException { |
810 |
|
sync.acquire(1); |
811 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
810 |
< |
sync.release(1); |
811 |
> |
c.awaitNanos(LONG_DELAY_MS * 1000L * 1000L); |
812 |
|
}}); |
813 |
|
|
814 |
|
t.start(); |
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)); |
831 |
– |
sync.release(1); |
832 |
|
}}); |
833 |
|
|
834 |
|
t.start(); |
842 |
|
* signalAll wakes up all threads |
843 |
|
*/ |
844 |
|
public void testSignalAll() throws InterruptedException { |
845 |
< |
final Mutex sync = new Mutex(); |
845 |
> |
final Mutex sync = new Mutex(); |
846 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
847 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
847 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
848 |
|
public void realRun() throws InterruptedException { |
849 |
|
sync.acquire(1); |
850 |
|
c.await(); |
851 |
|
sync.release(1); |
852 |
|
}}); |
853 |
|
|
854 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
854 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
855 |
|
public void realRun() throws InterruptedException { |
856 |
|
sync.acquire(1); |
857 |
|
c.await(); |
907 |
|
* tryReleaseShared setting state changes getState |
908 |
|
*/ |
909 |
|
public void testGetStateWithReleaseShared() { |
910 |
< |
final BooleanLatch l = new BooleanLatch(); |
911 |
< |
assertFalse(l.isSignalled()); |
912 |
< |
l.releaseShared(0); |
913 |
< |
assertTrue(l.isSignalled()); |
910 |
> |
final BooleanLatch l = new BooleanLatch(); |
911 |
> |
assertFalse(l.isSignalled()); |
912 |
> |
l.releaseShared(0); |
913 |
> |
assertTrue(l.isSignalled()); |
914 |
|
} |
915 |
|
|
916 |
|
/** |
917 |
|
* releaseShared has no effect when already signalled |
918 |
|
*/ |
919 |
|
public void testReleaseShared() { |
920 |
< |
final BooleanLatch l = new BooleanLatch(); |
921 |
< |
assertFalse(l.isSignalled()); |
922 |
< |
l.releaseShared(0); |
923 |
< |
assertTrue(l.isSignalled()); |
924 |
< |
l.releaseShared(0); |
925 |
< |
assertTrue(l.isSignalled()); |
920 |
> |
final BooleanLatch l = new BooleanLatch(); |
921 |
> |
assertFalse(l.isSignalled()); |
922 |
> |
l.releaseShared(0); |
923 |
> |
assertTrue(l.isSignalled()); |
924 |
> |
l.releaseShared(0); |
925 |
> |
assertTrue(l.isSignalled()); |
926 |
|
} |
927 |
|
|
928 |
|
/** |
929 |
|
* acquireSharedInterruptibly returns after release, but not before |
930 |
|
*/ |
931 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
932 |
< |
final BooleanLatch l = new BooleanLatch(); |
932 |
> |
final BooleanLatch l = new BooleanLatch(); |
933 |
|
|
934 |
< |
Thread t = new Thread(new CheckedRunnable() { |
934 |
> |
Thread t = new Thread(new CheckedRunnable() { |
935 |
|
public void realRun() throws InterruptedException { |
936 |
|
threadAssertFalse(l.isSignalled()); |
937 |
|
l.acquireSharedInterruptibly(0); |
951 |
|
* acquireSharedTimed returns after release |
952 |
|
*/ |
953 |
|
public void testAsquireSharedTimed() throws InterruptedException { |
954 |
< |
final BooleanLatch l = new BooleanLatch(); |
954 |
> |
final BooleanLatch l = new BooleanLatch(); |
955 |
|
|
956 |
< |
Thread t = new Thread(new CheckedRunnable() { |
956 |
> |
Thread t = new Thread(new CheckedRunnable() { |
957 |
|
public void realRun() throws InterruptedException { |
958 |
|
threadAssertFalse(l.isSignalled()); |
959 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
959 |
> |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS * 1000L * 1000L)); |
960 |
|
threadAssertTrue(l.isSignalled()); |
961 |
|
}}); |
962 |
|
|
973 |
|
*/ |
974 |
|
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
975 |
|
final BooleanLatch l = new BooleanLatch(); |
976 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
977 |
< |
public void realRun() throws InterruptedException { |
976 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
977 |
> |
public void realRun() throws InterruptedException { |
978 |
|
threadAssertFalse(l.isSignalled()); |
979 |
|
l.acquireSharedInterruptibly(0); |
980 |
|
}}); |
981 |
|
|
982 |
< |
t.start(); |
982 |
> |
t.start(); |
983 |
|
assertFalse(l.isSignalled()); |
984 |
|
t.interrupt(); |
985 |
|
t.join(); |
990 |
|
*/ |
991 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
992 |
|
final BooleanLatch l = new BooleanLatch(); |
993 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
< |
public void realRun() throws InterruptedException { |
993 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
994 |
> |
public void realRun() throws InterruptedException { |
995 |
|
threadAssertFalse(l.isSignalled()); |
996 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
996 |
> |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L); |
997 |
|
}}); |
998 |
|
|
999 |
|
t.start(); |
1008 |
|
*/ |
1009 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1010 |
|
final BooleanLatch l = new BooleanLatch(); |
1011 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1011 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1012 |
|
public void realRun() throws InterruptedException { |
1013 |
|
threadAssertFalse(l.isSignalled()); |
1014 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1014 |
> |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS * 1000L * 1000L)); |
1015 |
|
}}); |
1016 |
|
|
1017 |
|
t.start(); |