Example usage for java.util.concurrent.locks Condition await

List of usage examples for java.util.concurrent.locks Condition await

Introduction

In this page you can find the example usage for java.util.concurrent.locks Condition await.

Prototype

boolean await(long time, TimeUnit unit) throws InterruptedException;

Source Link

Document

Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.

Usage

From source file:com.yahoo.gondola.container.Utils.java

private static void wait(Lock lock, Condition condition, long timeout) throws InterruptedException {
    if (lock != null) {
        condition.await(timeout, TimeUnit.MILLISECONDS);
    } else {//from w  ww  .jav a2 s  .c o m
        Thread.sleep(timeout);
    }
}

From source file:Main.java

/**
 * Await for condition, handling//from   www . ja  v  a  2 s.  co m
 * <a href="http://errorprone.info/bugpattern/WaitNotInLoop">spurious wakeups</a>.
 * @param condition condition to await for
 * @param timeout the maximum time to wait in milliseconds
 * @return value from {@link Condition#await(long, TimeUnit)}
 */
public static boolean await(final Condition condition, final long timeout) throws InterruptedException {
    boolean awaited = false;
    long timeoutRemaining = timeout;
    long awaitStarted = System.currentTimeMillis();
    while (!awaited && timeoutRemaining > 0) {
        awaited = condition.await(timeoutRemaining, TimeUnit.MILLISECONDS);
        timeoutRemaining -= System.currentTimeMillis() - awaitStarted;
    }
    return awaited;
}

From source file:DeadlockDetectingLock.java

private static void awaitSeconds(Condition c, int seconds) {
    try {//  w w  w. ja v  a  2 s . c  o m
        c.await(seconds, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
    }
}

From source file:org.openqa.selenium.server.FrameGroupCommandQueueSet.java

/**
 *  Waits on the condition, making sure to wait at least as
 *  many seconds as specified, unless the condition is signaled
 *  first.//w  w  w.  j  a  v a  2 s .c om
 *
 *@param condition
 *@param numSeconds
 **/
protected static boolean waitUntilSignalOrNumSecondsPassed(Condition condition, int numSeconds) {
    boolean result = false;
    if (numSeconds > 0) {
        long now = System.currentTimeMillis();
        long deadline = now + (numSeconds * 1000);
        while (now < deadline) {
            try {
                LOGGER.debug("waiting for condition for " + (deadline - now) + " more ms");
                result = condition.await(deadline - now, TimeUnit.MILLISECONDS);
                LOGGER.debug("got condition? : " + result);
                now = deadline;
            } catch (InterruptedException ie) {
                now = System.currentTimeMillis();
            }
        }
    }
    return result;
}