/** tryAcquireSharedNanos times out if not released before timeout */ public void testTryAcquireSharedNanos_Timeout() { final BooleanLatch l = new BooleanLatch(); final BooleanLatch observedQueued = new BooleanLatch(); final long timeoutMillis = timeoutMillis(); Thread t = newStartedThread( new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); for (long millis = timeoutMillis(); !observedQueued.isSignalled(); millis *= 2) { long nanos = MILLISECONDS.toNanos(millis); long startTime = System.nanoTime(); assertFalse(l.tryAcquireSharedNanos(0, nanos)); assertTrue(millisElapsedSince(startTime) >= millis); } assertFalse(l.isSignalled()); } }); waitForQueuedThread(l, t); observedQueued.releaseShared(0); assertFalse(l.isSignalled()); awaitTermination(t); assertFalse(l.isSignalled()); }
/** releaseShared has no effect when already signalled */ public void testReleaseShared() { final BooleanLatch l = new BooleanLatch(); assertFalse(l.isSignalled()); assertTrue(l.releaseShared(0)); assertTrue(l.isSignalled()); assertTrue(l.releaseShared(0)); assertTrue(l.isSignalled()); }
/** acquireSharedInterruptibly is interruptible */ public void testAcquireSharedInterruptibly_Interruptible() { final BooleanLatch l = new BooleanLatch(); Thread t = newStartedThread( new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); l.acquireSharedInterruptibly(0); } }); waitForQueuedThread(l, t); assertFalse(l.isSignalled()); t.interrupt(); awaitTermination(t); assertFalse(l.isSignalled()); }
/** tryAcquireSharedNanos is interruptible */ public void testTryAcquireSharedNanos_Interruptible() { final BooleanLatch l = new BooleanLatch(); Thread t = newStartedThread( new CheckedInterruptedRunnable() { public void realRun() throws InterruptedException { assertFalse(l.isSignalled()); long nanos = MILLISECONDS.toNanos(2 * LONG_DELAY_MS); l.tryAcquireSharedNanos(0, nanos); } }); waitForQueuedThread(l, t); assertFalse(l.isSignalled()); t.interrupt(); awaitTermination(t); assertFalse(l.isSignalled()); }