コード例 #1
0
  /** hasWaiters returns true when a thread is waiting, else false */
  public void testHasWaiters() {
    final Mutex sync = new Mutex();
    final ConditionObject c = sync.newCondition();
    final BooleanLatch acquired = new BooleanLatch();
    Thread t =
        newStartedThread(
            new CheckedRunnable() {
              public void realRun() throws InterruptedException {
                sync.acquire();
                assertHasWaitersLocked(sync, c, NO_THREADS);
                assertFalse(sync.hasWaiters(c));
                assertTrue(acquired.releaseShared(0));
                c.await();
                sync.release();
              }
            });

    acquired.acquireShared(0);
    sync.acquire();
    assertHasWaitersLocked(sync, c, t);
    assertHasExclusiveQueuedThreads(sync, NO_THREADS);
    assertTrue(sync.hasWaiters(c));
    c.signal();
    assertHasWaitersLocked(sync, c, NO_THREADS);
    assertHasExclusiveQueuedThreads(sync, t);
    assertFalse(sync.hasWaiters(c));
    sync.release();

    awaitTermination(t);
    assertHasWaitersUnlocked(sync, c, NO_THREADS);
  }
コード例 #2
0
 /** hasWaiters(null) throws NullPointerException */
 public void testHasWaitersNPE() {
   final Mutex sync = new Mutex();
   try {
     sync.hasWaiters(null);
     shouldThrow();
   } catch (NullPointerException success) {
   }
 }
コード例 #3
0
 /** Checks that condition c has exactly the given waiter threads. */
 void assertHasWaitersLocked(Mutex sync, ConditionObject c, Thread... threads) {
   assertEquals(threads.length > 0, sync.hasWaiters(c));
   assertEquals(threads.length, sync.getWaitQueueLength(c));
   assertEquals(threads.length == 0, sync.getWaitingThreads(c).isEmpty());
   assertEquals(threads.length, sync.getWaitingThreads(c).size());
   assertEquals(
       new HashSet<Thread>(sync.getWaitingThreads(c)),
       new HashSet<Thread>(Arrays.asList(threads)));
 }
コード例 #4
0
 /** hasWaiters throws IllegalMonitorStateException if not synced */
 public void testHasWaitersIMSE() {
   final Mutex sync = new Mutex();
   final ConditionObject c = sync.newCondition();
   try {
     sync.hasWaiters(c);
     shouldThrow();
   } catch (IllegalMonitorStateException success) {
   }
   assertHasWaitersUnlocked(sync, c, NO_THREADS);
 }