当前位置: 首页>>代码示例>>Java>>正文


Java ConditionObject.await方法代码示例

本文整理汇总了Java中java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject.await方法的典型用法代码示例。如果您正苦于以下问题:Java ConditionObject.await方法的具体用法?Java ConditionObject.await怎么用?Java ConditionObject.await使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject的用法示例。


在下文中一共展示了ConditionObject.await方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: await

import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; //导入方法依赖的package包/类
/**
 * Awaits condition using the specified AwaitMethod.
 */
void await(ConditionObject c, AwaitMethod awaitMethod)
        throws InterruptedException {
    long timeoutMillis = 2 * LONG_DELAY_MS;
    switch (awaitMethod) {
    case await:
        c.await();
        break;
    case awaitTimed:
        assertTrue(c.await(timeoutMillis, MILLISECONDS));
        break;
    case awaitNanos:
        long nanosTimeout = MILLISECONDS.toNanos(timeoutMillis);
        long nanosRemaining = c.awaitNanos(nanosTimeout);
        assertTrue(nanosRemaining > 0);
        break;
    case awaitUntil:
        assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
        break;
    default:
        throw new AssertionError();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:AbstractQueuedLongSynchronizerTest.java

示例2: testGetWaitingThreads

import java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject; //导入方法依赖的package包/类
/**
 * getWaitingThreads returns only and all waiting threads
 */
public void testGetWaitingThreads() {
    final Mutex sync = new Mutex();
    final 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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:71,代码来源:AbstractQueuedLongSynchronizerTest.java


注:本文中的java.util.concurrent.locks.AbstractQueuedLongSynchronizer.ConditionObject.await方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。