本文整理汇总了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();
}
}
示例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);
}