List of usage examples for java.util.concurrent.locks Condition awaitUntil
boolean awaitUntil(Date deadline) throws InterruptedException;
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; }