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

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

Introduction

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

Prototype

boolean awaitUntil(Date deadline) throws InterruptedException;

Source Link

Document

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

Usage

From source file:org.sparkbit.jsonrpc.JSONRPCController.java

public boolean waitForTxBroadcast(String txid) {
    int n = jetty.sendAssetTimeout;
    if (n == 0)/*from  w  w  w. ja v a2  s.co m*/
        return true; // don't wait if timeout period is 0

    Date deadline = new DateTime().plusMillis(n).toDate();
    final StopWatch stopwatch = new StopWatch();
    stopwatch.start();
    boolean notYetElapsed;

    txBroadcastLock.lock();
    try {
        Condition c = txBroadcastLock.newCondition();
        txBroadcastMap.put(txid, c);
        while (txBroadcastMap.containsKey(txid)) {
            notYetElapsed = c.awaitUntil(deadline);
            if (!notYetElapsed) {
                log.debug("Peer broadcast waiting timed out for txid " + txid);
                return false;
            }
        }
    } catch (InterruptedException e) {
        return false;
    } finally {
        stopwatch.stop();
        txBroadcastLock.unlock();
        txBroadcastMap.remove(txid); // remove txid, we are done with it whatever happens.
    }
    log.debug("Peer broadcast waiting took " + stopwatch + " for txid " + txid);
    return true;
}