AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer implements java.io.Serializable { public final Collection<Thread> getWaitingThreads(ConditionObject condition) { if (!owns(condition)) throw new IllegalArgumentException("Not owner"); return condition.getWaitingThreads(); } protected  AbstractQueuedSynchronizer(); final void acquire(long arg); final void acquireInterruptibly(long arg); final boolean tryAcquireNanos(long arg, long nanosTimeout); final boolean release(long arg); final void acquireShared(long arg); final void acquireSharedInterruptibly(long arg); final boolean tryAcquireSharedNanos(long arg, long nanosTimeout); final boolean releaseShared(long arg); final boolean hasQueuedThreads(); final boolean hasContended(); final Thread getFirstQueuedThread(); final boolean isQueued(Thread thread); final boolean hasQueuedPredecessors(); final int getQueueLength(); final Collection<Thread> getQueuedThreads(); final Collection<Thread> getExclusiveQueuedThreads(); final Collection<Thread> getSharedQueuedThreads(); String toString(); final boolean owns(ConditionObject condition); final boolean hasWaiters(ConditionObject condition); final int getWaitQueueLength(ConditionObject condition); final Collection<Thread> getWaitingThreads(ConditionObject condition);  }
@Test public void testGetWaitingThreadsNPE() { final Mutex sync = new Mutex(); try { sync.getWaitingThreads(null); shouldThrow(); } catch (NullPointerException success) {} }
@Test public void testGetWaitingThreadsIAE() { final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final Mutex sync2 = new Mutex(); try { sync2.getWaitingThreads(c); shouldThrow(); } catch (IllegalArgumentException success) {} assertHasWaitersUnlocked(sync, c, NO_THREADS); }
@Test public void testGetWaitingThreadsIMSE() { final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); try { sync.getWaitingThreads(c); shouldThrow(); } catch (IllegalMonitorStateException success) {} assertHasWaitersUnlocked(sync, c, NO_THREADS); }
@Test public void testGetWaitingThreads() { final Mutex sync = new Mutex(); final AbstractQueuedSynchronizer.ConditionObject c = sync.newCondition(); final BooleanLatch acquired1 = new BooleanLatch(); final BooleanLatch acquired2 = new BooleanLatch(); final Thread t1 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(); assertHasWaitersLocked(sync, c, NO_THREADS); assertTrue(sync.getWaitingThreads(c).isEmpty()); assertTrue(acquired1.releaseShared(0)); c.await(); sync.release(); }}); final Thread t2 = new Thread(new CheckedRunnable() { public void realRun() throws InterruptedException { sync.acquire(); assertHasWaitersLocked(sync, c, t1); assertTrue(sync.getWaitingThreads(c).contains(t1)); assertFalse(sync.getWaitingThreads(c).isEmpty()); assertEquals(1, sync.getWaitingThreads(c).size()); assertTrue(acquired2.releaseShared(0)); c.await(); sync.release(); }}); sync.acquire(); assertHasWaitersLocked(sync, c, NO_THREADS); assertFalse(sync.getWaitingThreads(c).contains(t1)); assertFalse(sync.getWaitingThreads(c).contains(t2)); assertTrue(sync.getWaitingThreads(c).isEmpty()); assertEquals(0, sync.getWaitingThreads(c).size()); sync.release(); t1.start(); acquired1.acquireShared(0); sync.acquire(); assertHasWaitersLocked(sync, c, t1); assertTrue(sync.getWaitingThreads(c).contains(t1)); assertFalse(sync.getWaitingThreads(c).contains(t2)); assertFalse(sync.getWaitingThreads(c).isEmpty()); assertEquals(1, sync.getWaitingThreads(c).size()); sync.release(); t2.start(); acquired2.acquireShared(0); sync.acquire(); assertHasWaitersLocked(sync, c, t1, t2); assertHasExclusiveQueuedThreads(sync, NO_THREADS); assertTrue(sync.getWaitingThreads(c).contains(t1)); assertTrue(sync.getWaitingThreads(c).contains(t2)); assertFalse(sync.getWaitingThreads(c).isEmpty()); assertEquals(2, sync.getWaitingThreads(c).size()); c.signalAll(); assertHasWaitersLocked(sync, c, NO_THREADS); assertHasExclusiveQueuedThreads(sync, t1, t2); assertFalse(sync.getWaitingThreads(c).contains(t1)); assertFalse(sync.getWaitingThreads(c).contains(t2)); assertTrue(sync.getWaitingThreads(c).isEmpty()); assertEquals(0, sync.getWaitingThreads(c).size()); sync.release(); awaitTermination(t1); awaitTermination(t2); assertHasWaitersUnlocked(sync, c, NO_THREADS); }