1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
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 |
|
|
17 |
|
public class AbstractQueuedLongSynchronizerTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractQueuedLongSynchronizerTest.class); |
47 |
|
return true; |
48 |
|
} |
49 |
|
|
50 |
< |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { return new AbstractQueuedLongSynchronizer.ConditionObject(); } |
50 |
> |
public AbstractQueuedLongSynchronizer.ConditionObject newCondition() { |
51 |
> |
return new AbstractQueuedLongSynchronizer.ConditionObject(); |
52 |
> |
} |
53 |
|
|
54 |
|
} |
55 |
|
|
61 |
|
public boolean isSignalled() { return getState() != 0; } |
62 |
|
|
63 |
|
public long tryAcquireShared(long ignore) { |
64 |
< |
return isSignalled()? 1 : -1; |
64 |
> |
return isSignalled() ? 1 : -1; |
65 |
|
} |
66 |
|
|
67 |
|
public boolean tryReleaseShared(long ignore) { |
99 |
|
* isHeldExclusively is false upon construction |
100 |
|
*/ |
101 |
|
public void testIsHeldExclusively() { |
102 |
< |
Mutex rl = new Mutex(); |
102 |
> |
Mutex rl = new Mutex(); |
103 |
|
assertFalse(rl.isHeldExclusively()); |
104 |
|
} |
105 |
|
|
107 |
|
* acquiring released sync succeeds |
108 |
|
*/ |
109 |
|
public void testAcquire() { |
110 |
< |
Mutex rl = new Mutex(); |
110 |
> |
Mutex rl = new Mutex(); |
111 |
|
rl.acquire(1); |
112 |
|
assertTrue(rl.isHeldExclusively()); |
113 |
|
rl.release(1); |
118 |
|
* tryAcquire on an released sync succeeds |
119 |
|
*/ |
120 |
|
public void testTryAcquire() { |
121 |
< |
Mutex rl = new Mutex(); |
121 |
> |
Mutex rl = new Mutex(); |
122 |
|
assertTrue(rl.tryAcquire(1)); |
123 |
|
assertTrue(rl.isHeldExclusively()); |
124 |
|
rl.release(1); |
128 |
|
* hasQueuedThreads reports whether there are waiting threads |
129 |
|
*/ |
130 |
|
public void testhasQueuedThreads() throws InterruptedException { |
131 |
< |
final Mutex sync = new Mutex(); |
131 |
> |
final Mutex sync = new Mutex(); |
132 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
133 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
134 |
|
assertFalse(sync.hasQueuedThreads()); |
153 |
|
* isQueued(null) throws NPE |
154 |
|
*/ |
155 |
|
public void testIsQueuedNPE() { |
156 |
< |
final Mutex sync = new Mutex(); |
156 |
> |
final Mutex sync = new Mutex(); |
157 |
|
try { |
158 |
|
sync.isQueued(null); |
159 |
|
shouldThrow(); |
164 |
|
* isQueued reports whether a thread is queued. |
165 |
|
*/ |
166 |
|
public void testIsQueued() throws InterruptedException { |
167 |
< |
final Mutex sync = new Mutex(); |
167 |
> |
final Mutex sync = new Mutex(); |
168 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
169 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
170 |
|
assertFalse(sync.isQueued(t1)); |
194 |
|
* getFirstQueuedThread returns first waiting thread or null if none |
195 |
|
*/ |
196 |
|
public void testGetFirstQueuedThread() throws InterruptedException { |
197 |
< |
final Mutex sync = new Mutex(); |
197 |
> |
final Mutex sync = new Mutex(); |
198 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
199 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
200 |
|
assertNull(sync.getFirstQueuedThread()); |
221 |
|
* hasContended reports false if no thread has ever blocked, else true |
222 |
|
*/ |
223 |
|
public void testHasContended() throws InterruptedException { |
224 |
< |
final Mutex sync = new Mutex(); |
224 |
> |
final Mutex sync = new Mutex(); |
225 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
226 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
227 |
|
assertFalse(sync.hasContended()); |
246 |
|
* getQueuedThreads includes waiting threads |
247 |
|
*/ |
248 |
|
public void testGetQueuedThreads() throws InterruptedException { |
249 |
< |
final Mutex sync = new Mutex(); |
249 |
> |
final Mutex sync = new Mutex(); |
250 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
251 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
252 |
|
assertTrue(sync.getQueuedThreads().isEmpty()); |
274 |
|
* getExclusiveQueuedThreads includes waiting threads |
275 |
|
*/ |
276 |
|
public void testGetExclusiveQueuedThreads() throws InterruptedException { |
277 |
< |
final Mutex sync = new Mutex(); |
277 |
> |
final Mutex sync = new Mutex(); |
278 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
279 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
280 |
|
assertTrue(sync.getExclusiveQueuedThreads().isEmpty()); |
302 |
|
* getSharedQueuedThreads does not include exclusively waiting threads |
303 |
|
*/ |
304 |
|
public void testGetSharedQueuedThreads() throws InterruptedException { |
305 |
< |
final Mutex sync = new Mutex(); |
305 |
> |
final Mutex sync = new Mutex(); |
306 |
|
Thread t1 = new Thread(new InterruptedSyncRunnable(sync)); |
307 |
|
Thread t2 = new Thread(new InterruptibleSyncRunnable(sync)); |
308 |
|
assertTrue(sync.getSharedQueuedThreads().isEmpty()); |
328 |
|
* tryAcquireNanos is interruptible. |
329 |
|
*/ |
330 |
|
public void testInterruptedException2() throws InterruptedException { |
331 |
< |
final Mutex sync = new Mutex(); |
332 |
< |
sync.acquire(1); |
333 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
331 |
> |
final Mutex sync = new Mutex(); |
332 |
> |
sync.acquire(1); |
333 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
335 |
< |
sync.tryAcquireNanos(1, MEDIUM_DELAY_MS * 1000 * 1000); |
335 |
> |
sync.tryAcquireNanos(1, MILLISECONDS.toNanos(MEDIUM_DELAY_MS)); |
336 |
|
}}); |
337 |
|
|
338 |
|
t.start(); |
339 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
340 |
|
t.interrupt(); |
341 |
|
t.join(); |
342 |
|
} |
346 |
|
* TryAcquire on exclusively held sync fails |
347 |
|
*/ |
348 |
|
public void testTryAcquireWhenSynced() throws InterruptedException { |
349 |
< |
final Mutex sync = new Mutex(); |
350 |
< |
sync.acquire(1); |
351 |
< |
Thread t = new Thread(new CheckedRunnable() { |
349 |
> |
final Mutex sync = new Mutex(); |
350 |
> |
sync.acquire(1); |
351 |
> |
Thread t = new Thread(new CheckedRunnable() { |
352 |
|
public void realRun() { |
353 |
< |
threadAssertFalse(sync.tryAcquire(1)); |
353 |
> |
assertFalse(sync.tryAcquire(1)); |
354 |
|
}}); |
355 |
|
|
356 |
|
t.start(); |
362 |
|
* tryAcquireNanos on an exclusively held sync times out |
363 |
|
*/ |
364 |
|
public void testAcquireNanos_Timeout() throws InterruptedException { |
365 |
< |
final Mutex sync = new Mutex(); |
366 |
< |
sync.acquire(1); |
367 |
< |
Thread t = new Thread(new CheckedRunnable() { |
365 |
> |
final Mutex sync = new Mutex(); |
366 |
> |
sync.acquire(1); |
367 |
> |
Thread t = new Thread(new CheckedRunnable() { |
368 |
|
public void realRun() throws InterruptedException { |
369 |
< |
threadAssertFalse(sync.tryAcquireNanos(1, 1000 * 1000)); |
369 |
> |
long nanos = MILLISECONDS.toNanos(SHORT_DELAY_MS); |
370 |
> |
assertFalse(sync.tryAcquireNanos(1, nanos)); |
371 |
|
}}); |
372 |
|
|
373 |
|
t.start(); |
380 |
|
* getState is true when acquired and false when not |
381 |
|
*/ |
382 |
|
public void testGetState() throws InterruptedException { |
383 |
< |
final Mutex sync = new Mutex(); |
384 |
< |
sync.acquire(1); |
385 |
< |
assertTrue(sync.isHeldExclusively()); |
386 |
< |
sync.release(1); |
387 |
< |
assertFalse(sync.isHeldExclusively()); |
388 |
< |
Thread t = new Thread(new CheckedRunnable() { |
383 |
> |
final Mutex sync = new Mutex(); |
384 |
> |
sync.acquire(1); |
385 |
> |
assertTrue(sync.isHeldExclusively()); |
386 |
> |
sync.release(1); |
387 |
> |
assertFalse(sync.isHeldExclusively()); |
388 |
> |
Thread t = new Thread(new CheckedRunnable() { |
389 |
|
public void realRun() throws InterruptedException { |
390 |
|
sync.acquire(1); |
391 |
|
Thread.sleep(SMALL_DELAY_MS); |
404 |
|
* acquireInterruptibly is interruptible. |
405 |
|
*/ |
406 |
|
public void testAcquireInterruptibly1() throws InterruptedException { |
407 |
< |
final Mutex sync = new Mutex(); |
408 |
< |
sync.acquire(1); |
409 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
407 |
> |
final Mutex sync = new Mutex(); |
408 |
> |
sync.acquire(1); |
409 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
410 |
|
t.start(); |
411 |
|
Thread.sleep(SHORT_DELAY_MS); |
412 |
|
t.interrupt(); |
419 |
|
* acquireInterruptibly succeeds when released, else is interruptible |
420 |
|
*/ |
421 |
|
public void testAcquireInterruptibly2() throws InterruptedException { |
422 |
< |
final Mutex sync = new Mutex(); |
422 |
> |
final Mutex sync = new Mutex(); |
423 |
|
sync.acquireInterruptibly(1); |
424 |
< |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
424 |
> |
Thread t = new Thread(new InterruptedSyncRunnable(sync)); |
425 |
|
t.start(); |
426 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
427 |
|
t.interrupt(); |
428 |
|
assertTrue(sync.isHeldExclusively()); |
429 |
|
t.join(); |
433 |
|
* owns is true for a condition created by sync else false |
434 |
|
*/ |
435 |
|
public void testOwns() { |
436 |
< |
final Mutex sync = new Mutex(); |
436 |
> |
final Mutex sync = new Mutex(); |
437 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
438 |
|
final Mutex sync2 = new Mutex(); |
439 |
|
assertTrue(sync.owns(c)); |
444 |
|
* Calling await without holding sync throws IllegalMonitorStateException |
445 |
|
*/ |
446 |
|
public void testAwait_IllegalMonitor() throws InterruptedException { |
447 |
< |
final Mutex sync = new Mutex(); |
447 |
> |
final Mutex sync = new Mutex(); |
448 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
449 |
|
try { |
450 |
|
c.await(); |
456 |
|
* Calling signal without holding sync throws IllegalMonitorStateException |
457 |
|
*/ |
458 |
|
public void testSignal_IllegalMonitor() throws InterruptedException { |
459 |
< |
final Mutex sync = new Mutex(); |
459 |
> |
final Mutex sync = new Mutex(); |
460 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
461 |
|
try { |
462 |
|
c.signal(); |
468 |
|
* awaitNanos without a signal times out |
469 |
|
*/ |
470 |
|
public void testAwaitNanos_Timeout() throws InterruptedException { |
471 |
< |
final Mutex sync = new Mutex(); |
471 |
> |
final Mutex sync = new Mutex(); |
472 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
473 |
|
sync.acquire(1); |
474 |
|
long t = c.awaitNanos(100); |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* Timed await without a signal times out |
480 |
> |
* Timed await without a signal times out |
481 |
|
*/ |
482 |
|
public void testAwait_Timeout() throws InterruptedException { |
483 |
< |
final Mutex sync = new Mutex(); |
483 |
> |
final Mutex sync = new Mutex(); |
484 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
485 |
|
sync.acquire(1); |
486 |
< |
assertFalse(c.await(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
486 |
> |
assertFalse(c.await(SHORT_DELAY_MS, MILLISECONDS)); |
487 |
|
sync.release(1); |
488 |
|
} |
489 |
|
|
491 |
|
* awaitUntil without a signal times out |
492 |
|
*/ |
493 |
|
public void testAwaitUntil_Timeout() throws InterruptedException { |
494 |
< |
final Mutex sync = new Mutex(); |
494 |
> |
final Mutex sync = new Mutex(); |
495 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
496 |
|
sync.acquire(1); |
497 |
|
java.util.Date d = new java.util.Date(); |
503 |
|
* await returns when signalled |
504 |
|
*/ |
505 |
|
public void testAwait() throws InterruptedException { |
506 |
< |
final Mutex sync = new Mutex(); |
506 |
> |
final Mutex sync = new Mutex(); |
507 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
508 |
< |
Thread t = new Thread(new CheckedRunnable() { |
508 |
> |
Thread t = new Thread(new CheckedRunnable() { |
509 |
|
public void realRun() throws InterruptedException { |
510 |
|
sync.acquire(1); |
511 |
|
c.await(); |
527 |
|
* hasWaiters throws NPE if null |
528 |
|
*/ |
529 |
|
public void testHasWaitersNPE() { |
530 |
< |
final Mutex sync = new Mutex(); |
530 |
> |
final Mutex sync = new Mutex(); |
531 |
|
try { |
532 |
|
sync.hasWaiters(null); |
533 |
|
shouldThrow(); |
538 |
|
* getWaitQueueLength throws NPE if null |
539 |
|
*/ |
540 |
|
public void testGetWaitQueueLengthNPE() { |
541 |
< |
final Mutex sync = new Mutex(); |
541 |
> |
final Mutex sync = new Mutex(); |
542 |
|
try { |
543 |
|
sync.getWaitQueueLength(null); |
544 |
|
shouldThrow(); |
550 |
|
* getWaitingThreads throws NPE if null |
551 |
|
*/ |
552 |
|
public void testGetWaitingThreadsNPE() { |
553 |
< |
final Mutex sync = new Mutex(); |
553 |
> |
final Mutex sync = new Mutex(); |
554 |
|
try { |
555 |
|
sync.getWaitingThreads(null); |
556 |
|
shouldThrow(); |
562 |
|
* hasWaiters throws IAE if not owned |
563 |
|
*/ |
564 |
|
public void testHasWaitersIAE() { |
565 |
< |
final Mutex sync = new Mutex(); |
565 |
> |
final Mutex sync = new Mutex(); |
566 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
567 |
< |
final Mutex sync2 = new Mutex(); |
567 |
> |
final Mutex sync2 = new Mutex(); |
568 |
|
try { |
569 |
|
sync2.hasWaiters(c); |
570 |
|
shouldThrow(); |
575 |
|
* hasWaiters throws IMSE if not synced |
576 |
|
*/ |
577 |
|
public void testHasWaitersIMSE() { |
578 |
< |
final Mutex sync = new Mutex(); |
578 |
> |
final Mutex sync = new Mutex(); |
579 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
580 |
|
try { |
581 |
|
sync.hasWaiters(c); |
588 |
|
* getWaitQueueLength throws IAE if not owned |
589 |
|
*/ |
590 |
|
public void testGetWaitQueueLengthIAE() { |
591 |
< |
final Mutex sync = new Mutex(); |
591 |
> |
final Mutex sync = new Mutex(); |
592 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
593 |
< |
final Mutex sync2 = new Mutex(); |
593 |
> |
final Mutex sync2 = new Mutex(); |
594 |
|
try { |
595 |
|
sync2.getWaitQueueLength(c); |
596 |
|
shouldThrow(); |
601 |
|
* getWaitQueueLength throws IMSE if not synced |
602 |
|
*/ |
603 |
|
public void testGetWaitQueueLengthIMSE() { |
604 |
< |
final Mutex sync = new Mutex(); |
604 |
> |
final Mutex sync = new Mutex(); |
605 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
606 |
|
try { |
607 |
|
sync.getWaitQueueLength(c); |
614 |
|
* getWaitingThreads throws IAE if not owned |
615 |
|
*/ |
616 |
|
public void testGetWaitingThreadsIAE() { |
617 |
< |
final Mutex sync = new Mutex(); |
617 |
> |
final Mutex sync = new Mutex(); |
618 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
619 |
< |
final Mutex sync2 = new Mutex(); |
619 |
> |
final Mutex sync2 = new Mutex(); |
620 |
|
try { |
621 |
|
sync2.getWaitingThreads(c); |
622 |
|
shouldThrow(); |
627 |
|
* getWaitingThreads throws IMSE if not synced |
628 |
|
*/ |
629 |
|
public void testGetWaitingThreadsIMSE() { |
630 |
< |
final Mutex sync = new Mutex(); |
630 |
> |
final Mutex sync = new Mutex(); |
631 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
632 |
|
try { |
633 |
|
sync.getWaitingThreads(c); |
641 |
|
* hasWaiters returns true when a thread is waiting, else false |
642 |
|
*/ |
643 |
|
public void testHasWaiters() throws InterruptedException { |
644 |
< |
final Mutex sync = new Mutex(); |
644 |
> |
final Mutex sync = new Mutex(); |
645 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
646 |
< |
Thread t = new Thread(new CheckedRunnable() { |
646 |
> |
Thread t = new Thread(new CheckedRunnable() { |
647 |
|
public void realRun() throws InterruptedException { |
648 |
|
sync.acquire(1); |
649 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
650 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
649 |
> |
assertFalse(sync.hasWaiters(c)); |
650 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
651 |
|
c.await(); |
652 |
|
sync.release(1); |
653 |
|
}}); |
672 |
|
* getWaitQueueLength returns number of waiting threads |
673 |
|
*/ |
674 |
|
public void testGetWaitQueueLength() throws InterruptedException { |
675 |
< |
final Mutex sync = new Mutex(); |
675 |
> |
final Mutex sync = new Mutex(); |
676 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
677 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
677 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
678 |
|
public void realRun() throws InterruptedException { |
679 |
|
sync.acquire(1); |
680 |
< |
threadAssertFalse(sync.hasWaiters(c)); |
681 |
< |
threadAssertEquals(0, sync.getWaitQueueLength(c)); |
680 |
> |
assertFalse(sync.hasWaiters(c)); |
681 |
> |
assertEquals(0, sync.getWaitQueueLength(c)); |
682 |
|
c.await(); |
683 |
|
sync.release(1); |
684 |
|
}}); |
685 |
|
|
686 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
686 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
687 |
|
public void realRun() throws InterruptedException { |
688 |
|
sync.acquire(1); |
689 |
< |
threadAssertTrue(sync.hasWaiters(c)); |
690 |
< |
threadAssertEquals(1, sync.getWaitQueueLength(c)); |
689 |
> |
assertTrue(sync.hasWaiters(c)); |
690 |
> |
assertEquals(1, sync.getWaitQueueLength(c)); |
691 |
|
c.await(); |
692 |
|
sync.release(1); |
693 |
|
}}); |
716 |
|
* getWaitingThreads returns only and all waiting threads |
717 |
|
*/ |
718 |
|
public void testGetWaitingThreads() throws InterruptedException { |
719 |
< |
final Mutex sync = new Mutex(); |
719 |
> |
final Mutex sync = new Mutex(); |
720 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
721 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
721 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
722 |
|
public void realRun() throws InterruptedException { |
723 |
|
sync.acquire(1); |
724 |
< |
threadAssertTrue(sync.getWaitingThreads(c).isEmpty()); |
724 |
> |
assertTrue(sync.getWaitingThreads(c).isEmpty()); |
725 |
|
c.await(); |
726 |
|
sync.release(1); |
727 |
|
}}); |
728 |
|
|
729 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
729 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
730 |
|
public void realRun() throws InterruptedException { |
731 |
|
sync.acquire(1); |
732 |
< |
threadAssertFalse(sync.getWaitingThreads(c).isEmpty()); |
732 |
> |
assertFalse(sync.getWaitingThreads(c).isEmpty()); |
733 |
|
c.await(); |
734 |
|
sync.release(1); |
735 |
|
}}); |
764 |
|
* awaitUninterruptibly doesn't abort on interrupt |
765 |
|
*/ |
766 |
|
public void testAwaitUninterruptibly() throws InterruptedException { |
767 |
< |
final Mutex sync = new Mutex(); |
767 |
> |
final Mutex sync = new Mutex(); |
768 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
769 |
< |
Thread t = new Thread(new CheckedRunnable() { |
769 |
> |
Thread t = new Thread(new CheckedRunnable() { |
770 |
|
public void realRun() { |
771 |
|
sync.acquire(1); |
772 |
|
c.awaitUninterruptibly(); |
787 |
|
* await is interruptible |
788 |
|
*/ |
789 |
|
public void testAwait_Interrupt() throws InterruptedException { |
790 |
< |
final Mutex sync = new Mutex(); |
790 |
> |
final Mutex sync = new Mutex(); |
791 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
792 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
< |
public void realRun() throws InterruptedException { |
792 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
|
sync.acquire(1); |
795 |
|
c.await(); |
790 |
– |
sync.release(1); |
796 |
|
}}); |
797 |
|
|
798 |
|
t.start(); |
806 |
|
* awaitNanos is interruptible |
807 |
|
*/ |
808 |
|
public void testAwaitNanos_Interrupt() throws InterruptedException { |
809 |
< |
final Mutex sync = new Mutex(); |
809 |
> |
final Mutex sync = new Mutex(); |
810 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
811 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
< |
public void realRun() throws InterruptedException { |
811 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
812 |
> |
public void realRun() throws InterruptedException { |
813 |
|
sync.acquire(1); |
814 |
< |
c.awaitNanos(1000 * 1000 * 1000); // 1 sec |
810 |
< |
sync.release(1); |
814 |
> |
c.awaitNanos(MILLISECONDS.toNanos(LONG_DELAY_MS)); |
815 |
|
}}); |
816 |
|
|
817 |
|
t.start(); |
825 |
|
* awaitUntil is interruptible |
826 |
|
*/ |
827 |
|
public void testAwaitUntil_Interrupt() throws InterruptedException { |
828 |
< |
final Mutex sync = new Mutex(); |
828 |
> |
final Mutex sync = new Mutex(); |
829 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
830 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
< |
public void realRun() throws InterruptedException { |
830 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
831 |
> |
public void realRun() throws InterruptedException { |
832 |
|
sync.acquire(1); |
833 |
|
java.util.Date d = new java.util.Date(); |
834 |
|
c.awaitUntil(new java.util.Date(d.getTime() + 10000)); |
831 |
– |
sync.release(1); |
835 |
|
}}); |
836 |
|
|
837 |
|
t.start(); |
845 |
|
* signalAll wakes up all threads |
846 |
|
*/ |
847 |
|
public void testSignalAll() throws InterruptedException { |
848 |
< |
final Mutex sync = new Mutex(); |
848 |
> |
final Mutex sync = new Mutex(); |
849 |
|
final AbstractQueuedLongSynchronizer.ConditionObject c = sync.newCondition(); |
850 |
< |
Thread t1 = new Thread(new CheckedRunnable() { |
850 |
> |
Thread t1 = new Thread(new CheckedRunnable() { |
851 |
|
public void realRun() throws InterruptedException { |
852 |
|
sync.acquire(1); |
853 |
|
c.await(); |
854 |
|
sync.release(1); |
855 |
|
}}); |
856 |
|
|
857 |
< |
Thread t2 = new Thread(new CheckedRunnable() { |
857 |
> |
Thread t2 = new Thread(new CheckedRunnable() { |
858 |
|
public void realRun() throws InterruptedException { |
859 |
|
sync.acquire(1); |
860 |
|
c.await(); |
910 |
|
* tryReleaseShared setting state changes getState |
911 |
|
*/ |
912 |
|
public void testGetStateWithReleaseShared() { |
913 |
< |
final BooleanLatch l = new BooleanLatch(); |
914 |
< |
assertFalse(l.isSignalled()); |
915 |
< |
l.releaseShared(0); |
916 |
< |
assertTrue(l.isSignalled()); |
913 |
> |
final BooleanLatch l = new BooleanLatch(); |
914 |
> |
assertFalse(l.isSignalled()); |
915 |
> |
l.releaseShared(0); |
916 |
> |
assertTrue(l.isSignalled()); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* releaseShared has no effect when already signalled |
921 |
|
*/ |
922 |
|
public void testReleaseShared() { |
923 |
< |
final BooleanLatch l = new BooleanLatch(); |
924 |
< |
assertFalse(l.isSignalled()); |
925 |
< |
l.releaseShared(0); |
926 |
< |
assertTrue(l.isSignalled()); |
927 |
< |
l.releaseShared(0); |
928 |
< |
assertTrue(l.isSignalled()); |
923 |
> |
final BooleanLatch l = new BooleanLatch(); |
924 |
> |
assertFalse(l.isSignalled()); |
925 |
> |
l.releaseShared(0); |
926 |
> |
assertTrue(l.isSignalled()); |
927 |
> |
l.releaseShared(0); |
928 |
> |
assertTrue(l.isSignalled()); |
929 |
|
} |
930 |
|
|
931 |
|
/** |
932 |
|
* acquireSharedInterruptibly returns after release, but not before |
933 |
|
*/ |
934 |
|
public void testAcquireSharedInterruptibly() throws InterruptedException { |
935 |
< |
final BooleanLatch l = new BooleanLatch(); |
935 |
> |
final BooleanLatch l = new BooleanLatch(); |
936 |
|
|
937 |
< |
Thread t = new Thread(new CheckedRunnable() { |
937 |
> |
Thread t = new Thread(new CheckedRunnable() { |
938 |
|
public void realRun() throws InterruptedException { |
939 |
< |
threadAssertFalse(l.isSignalled()); |
939 |
> |
assertFalse(l.isSignalled()); |
940 |
|
l.acquireSharedInterruptibly(0); |
941 |
< |
threadAssertTrue(l.isSignalled()); |
941 |
> |
assertTrue(l.isSignalled()); |
942 |
|
}}); |
943 |
|
|
944 |
|
t.start(); |
953 |
|
/** |
954 |
|
* acquireSharedTimed returns after release |
955 |
|
*/ |
956 |
< |
public void testAsquireSharedTimed() throws InterruptedException { |
957 |
< |
final BooleanLatch l = new BooleanLatch(); |
956 |
> |
public void testAcquireSharedTimed() throws InterruptedException { |
957 |
> |
final BooleanLatch l = new BooleanLatch(); |
958 |
|
|
959 |
< |
Thread t = new Thread(new CheckedRunnable() { |
959 |
> |
Thread t = new Thread(new CheckedRunnable() { |
960 |
|
public void realRun() throws InterruptedException { |
961 |
< |
threadAssertFalse(l.isSignalled()); |
962 |
< |
threadAssertTrue(l.tryAcquireSharedNanos(0, MEDIUM_DELAY_MS* 1000 * 1000)); |
963 |
< |
threadAssertTrue(l.isSignalled()); |
961 |
> |
assertFalse(l.isSignalled()); |
962 |
> |
long nanos = MILLISECONDS.toNanos(MEDIUM_DELAY_MS); |
963 |
> |
assertTrue(l.tryAcquireSharedNanos(0, nanos)); |
964 |
> |
assertTrue(l.isSignalled()); |
965 |
|
}}); |
966 |
|
|
967 |
|
t.start(); |
975 |
|
/** |
976 |
|
* acquireSharedInterruptibly throws IE if interrupted before released |
977 |
|
*/ |
978 |
< |
public void testAcquireSharedInterruptibly_InterruptedException() throws InterruptedException { |
978 |
> |
public void testAcquireSharedInterruptibly_InterruptedException() |
979 |
> |
throws InterruptedException { |
980 |
|
final BooleanLatch l = new BooleanLatch(); |
981 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
< |
public void realRun() throws InterruptedException { |
983 |
< |
threadAssertFalse(l.isSignalled()); |
981 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
982 |
> |
public void realRun() throws InterruptedException { |
983 |
> |
assertFalse(l.isSignalled()); |
984 |
|
l.acquireSharedInterruptibly(0); |
985 |
|
}}); |
986 |
|
|
987 |
< |
t.start(); |
987 |
> |
t.start(); |
988 |
|
assertFalse(l.isSignalled()); |
989 |
|
t.interrupt(); |
990 |
|
t.join(); |
995 |
|
*/ |
996 |
|
public void testAcquireSharedNanos_InterruptedException() throws InterruptedException { |
997 |
|
final BooleanLatch l = new BooleanLatch(); |
998 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
< |
public void realRun() throws InterruptedException { |
1000 |
< |
threadAssertFalse(l.isSignalled()); |
1001 |
< |
l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000); |
998 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
999 |
> |
public void realRun() throws InterruptedException { |
1000 |
> |
assertFalse(l.isSignalled()); |
1001 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1002 |
> |
l.tryAcquireSharedNanos(0, nanos); |
1003 |
|
}}); |
1004 |
|
|
1005 |
|
t.start(); |
1014 |
|
*/ |
1015 |
|
public void testAcquireSharedNanos_Timeout() throws InterruptedException { |
1016 |
|
final BooleanLatch l = new BooleanLatch(); |
1017 |
< |
Thread t = new Thread(new CheckedRunnable() { |
1017 |
> |
Thread t = new Thread(new CheckedRunnable() { |
1018 |
|
public void realRun() throws InterruptedException { |
1019 |
< |
threadAssertFalse(l.isSignalled()); |
1020 |
< |
threadAssertFalse(l.tryAcquireSharedNanos(0, SMALL_DELAY_MS* 1000 * 1000)); |
1019 |
> |
assertFalse(l.isSignalled()); |
1020 |
> |
long nanos = MILLISECONDS.toNanos(SMALL_DELAY_MS); |
1021 |
> |
assertFalse(l.tryAcquireSharedNanos(0, nanos)); |
1022 |
|
}}); |
1023 |
|
|
1024 |
|
t.start(); |