本文整理汇总了Java中java.util.concurrent.locks.Condition.await方法的典型用法代码示例。如果您正苦于以下问题:Java Condition.await方法的具体用法?Java Condition.await怎么用?Java Condition.await使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.Condition
的用法示例。
在下文中一共展示了Condition.await方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: awaitUntil
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void awaitUntil(SynchronizationEvent synchronizationEvent) throws InterruptedException, TimeoutException {
logger.debug("Before await " + synchronizationEvent.isSatisfied());
if (synchronizationEvent.isSatisfied()) {
return;
}
lock.lock();
Condition condition = lock.newCondition();
eventConditionMap.put(synchronizationEvent, condition);
logger.debug("When calling await " + synchronizationEvent.isSatisfied());
boolean timedOut = !condition.await(timeoutInSeconds, TimeUnit.SECONDS);
lock.unlock();
if (timedOut) {
logger.debug("After timeout " + synchronizationEvent.isSatisfied());
logger.error("Condition {} timed out! ", synchronizationEvent.getName());
throw new TimeoutException("The synchronization event " + synchronizationEvent.getName() + " was not satisfied in the specified time");
}
}
示例2: wait
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
/**
* Causes the current thread to wait until waiting time elapses.
*
* @param timeout the maximum time to wait in milliseconds
*
* @throws CdpException if the session held by another thread at the time of invocation.
*
* @return this
*/
public Session wait(int timeout, boolean log) {
if (lock.tryLock()) {
Condition condition = lock.newCondition();
try {
if (log) {
logEntry("wait", timeout + "ms");
}
condition.await(timeout, MILLISECONDS);
} catch (InterruptedException e) {
throw new CdpException(e);
} finally {
if (lock.isLocked()) {
lock.unlock();
}
}
} else {
throw new CdpException("Unable to acquire lock");
}
return getThis();
}
示例3: await
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
/**
* Awaits condition "indefinitely" using the specified AwaitMethod.
*/
void await(Condition 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 timeoutNanos = MILLISECONDS.toNanos(timeoutMillis);
long nanosRemaining = c.awaitNanos(timeoutNanos);
assertTrue(nanosRemaining > timeoutNanos / 2);
assertTrue(nanosRemaining <= timeoutNanos);
break;
case awaitUntil:
assertTrue(c.awaitUntil(delayedDate(timeoutMillis)));
break;
default:
throw new AssertionError();
}
}
示例4: startThrift
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void startThrift() throws Exception {
final Lock startLock = new ReentrantLock();
final Condition startCondition = startLock.newCondition();
final AtomicBoolean startedServing = new AtomicBoolean();
try (ServerSocket socket = new ServerSocket(0)) {
thriftPort = socket.getLocalPort();
}
conf.setVar(ConfVars.METASTOREURIS, getThriftConnectionUri());
final HiveConf hiveConf = new HiveConf(conf, HiveMetaStoreClient.class);
thriftServer.execute(new Runnable() {
@Override
public void run() {
try {
HadoopThriftAuthBridge bridge = new HadoopThriftAuthBridge23();
HiveMetaStore.startMetaStore(thriftPort, bridge, hiveConf, startLock, startCondition, startedServing);
} catch (Throwable e) {
LOG.error("Unable to start a Thrift server for Hive Metastore", e);
}
}
});
int i = 0;
while (i++ < 3) {
startLock.lock();
try {
if (startCondition.await(1, TimeUnit.MINUTES)) {
break;
}
} finally {
startLock.unlock();
}
if (i == 3) {
throw new RuntimeException("Maximum number of tries reached whilst waiting for Thrift server to be ready");
}
}
}
示例5: verifyNamespacedListIsBackedUpIntoProperFiles_WhenNamespacedListIsReloaded
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void verifyNamespacedListIsBackedUpIntoProperFiles_WhenNamespacedListIsReloaded() throws Exception {
deleteBackups();
contextA.setNamespacedLists(Arrays.asList(new TestNamespacedList("integrationTest", "1", "2")));
Lock listUpdateLock = new ReentrantLock();
Condition updated = listUpdateLock.newCondition();
newBatchAppliedListener.setCallback(event -> {
log.info("Namespaced List updated in data store");
if (event.getData().getNamespacedLists().containsKey("integrationTest")) {
listUpdateLock.lock();
try {
updated.signalAll();
} finally {
listUpdateLock.unlock();
}
}
});
testHelperA.getDataStore().putNamespacedList();
listUpdateLock.lock();
try {
updated.await(1, TimeUnit.SECONDS);
} finally {
listUpdateLock.unlock();
}
log.info("Unlocked test. Keep going");
Thread.sleep(1000); // TODO: find better way to wait till all backup files are written
Assert.assertTrue("Namespaced list backup is absent", Files.exists(Paths.get(currentPath + File.separator + "namespacedlists.json")));
}
示例6: backupsAreUpdatedAfterDataSourceComesUp
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void backupsAreUpdatedAfterDataSourceComesUp() throws Exception {
TestContext context = contextBuilder.withNamespacedList(NAMESPACED_LIST, "new value").build();
IntegrationTestHelper testHelper = prepareTestHelperForInitRedirectorModel(context);
Lock lock = new ReentrantLock();
Condition listUpdated = lock.newCondition();
newBatchAppliedListener.setCallback(event -> {
log.info("Namespaced Lists updated after zookeeper came up");
lock.lock();
try {
listUpdated.signalAll();
} finally {
lock.unlock();
}
});
dataStoreSupport.startZookeeper();
testHelper.getDataStore().setupEnvForContext();
lock.lock();
try {
listUpdated.await(3, TimeUnit.SECONDS);
} finally {
lock.unlock();
}
log.info("Unlocked test. Keep going");
NamespacedListsBatch namespacedListsFromBackup = testHelper.getBackups().getNamespacedLists();
Assert.assertTrue(namespacedListsFromBackup.getNamespacedLists().get(NAMESPACED_LIST).contains("new value"));
Assert.assertFalse(namespacedListsFromBackup.getNamespacedLists().get(NAMESPACED_LIST).contains(NAMESPACED_LIST_VALUE));
}
示例7: backupsAreUpdatedAfterNamespacedListIsChangedInDataStore
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
private void backupsAreUpdatedAfterNamespacedListIsChangedInDataStore() throws Exception {
TestContext context = contextBuilder.withNamespacedList("NewList", "value1", "value2").build();
IntegrationTestHelper testHelper = prepareTestHelperForUpdateRedirectorModel(context);
Lock lock = new ReentrantLock();
Condition listUpdated = lock.newCondition();
newBatchAppliedListener.setCallback(unchecked(event -> {
log.info("New namespaced list is applied: {}", event.getData().getNamespacedLists());
if (event.getData() != null && event.getData().getNamespacedLists() != null) {
if (event.getData().getNamespacedLists().containsKey("NewList")) {
log.info("Event for update of new namespaced list happened. Continue processing");
} else {
log.info("Skip this event");
return;
}
}
lock.lock();
try {
listUpdated.signalAll();
} finally {
lock.unlock();
}
}));
lock.lock();
try {
testHelper.getDataStore().putNamespacedList();
if (listUpdated.await(1, TimeUnit.SECONDS)) {
log.info("Namespaced list is applied. Going to verification");
} else {
log.info("Timeout exceed. Unlocking test");
}
} finally {
lock.unlock();
}
NamespacedListsBatch namespacedListsFromBackup = testHelper.getBackups().getNamespacedLists();
log.info("Got namespaced list from backup {}", namespacedListsFromBackup.getNamespacedLists());
Assert.assertTrue(namespacedListsFromBackup.getNamespacedLists().get("NewList").contains("value1"));
Assert.assertTrue(namespacedListsFromBackup.getNamespacedLists().get("NewList").contains("value2"));
}
示例8: testGetWaitingThreads
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitingThreads(boolean fair) {
final PublicReentrantReadWriteLock lock =
new PublicReentrantReadWriteLock(fair);
final Condition c = lock.writeLock().newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
locked1.countDown();
c.await();
lock.writeLock().unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.writeLock().lock();
assertFalse(lock.getWaitingThreads(c).isEmpty());
locked2.countDown();
c.await();
lock.writeLock().unlock();
}});
lock.writeLock().lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
lock.writeLock().unlock();
t1.start();
await(locked1);
t2.start();
await(locked2);
lock.writeLock().lock();
assertTrue(lock.hasWaiters(c));
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertTrue(lock.getWaitingThreads(c).contains(t2));
assertEquals(2, lock.getWaitingThreads(c).size());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.writeLock().unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
示例9: testGetWaitQueueLength
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitQueueLength(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.hasWaiters(c));
assertEquals(0, lock.getWaitQueueLength(c));
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.hasWaiters(c));
assertEquals(1, lock.getWaitQueueLength(c));
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertEquals(0, lock.getWaitQueueLength(c));
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertEquals(1, lock.getWaitQueueLength(c));
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertEquals(2, lock.getWaitQueueLength(c));
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}
示例10: testGetWaitingThreads
import java.util.concurrent.locks.Condition; //导入方法依赖的package包/类
public void testGetWaitingThreads(boolean fair) {
final PublicReentrantLock lock = new PublicReentrantLock(fair);
final Condition c = lock.newCondition();
final CountDownLatch locked1 = new CountDownLatch(1);
final CountDownLatch locked2 = new CountDownLatch(1);
Thread t1 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
locked1.countDown();
c.await();
lock.unlock();
}});
Thread t2 = new Thread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
lock.lock();
assertFalse(lock.getWaitingThreads(c).isEmpty());
locked2.countDown();
c.await();
lock.unlock();
}});
lock.lock();
assertTrue(lock.getWaitingThreads(c).isEmpty());
lock.unlock();
t1.start();
await(locked1);
lock.lock();
assertHasWaiters(lock, c, t1);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertFalse(lock.getWaitingThreads(c).contains(t2));
assertEquals(1, lock.getWaitingThreads(c).size());
lock.unlock();
t2.start();
await(locked2);
lock.lock();
assertHasWaiters(lock, c, t1, t2);
assertTrue(lock.getWaitingThreads(c).contains(t1));
assertTrue(lock.getWaitingThreads(c).contains(t2));
assertEquals(2, lock.getWaitingThreads(c).size());
c.signalAll();
assertHasNoWaiters(lock, c);
lock.unlock();
awaitTermination(t1);
awaitTermination(t2);
assertHasNoWaiters(lock, c);
}