@Nullable private T pollObject(long timeoutMs) throws InterruptedException { long nanos = TIME_UNIT.toNanos(timeoutMs); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (objects.isEmpty()) { if (nanos <= 0) { return null; } nanos = notEnough.awaitNanos(nanos); } return objects.pop(); } finally { lock.unlock(); } }
@Override public void resume() throws InterruptedException { pauseLock.lockInterruptibly(); try { pauseRequested = false; shouldResume.signalAll(); long nanos = TimeUnit.SECONDS.toNanos(5); while (isPaused()) { if (nanos <= 0L) { throw new RuntimeException("Resume command was not accepted within 5 seconds"); } nanos = shouldResume.awaitNanos(nanos); } } finally { pauseLock.unlock(); } }
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { for (;;) { if (runStateAtLeast(ctl.get(), TERMINATED)) return true; if (nanos <= 0) return false; nanos = termination.awaitNanos(nanos); } } finally { mainLock.unlock(); } }
final long nanos = TimeUnit.MILLISECONDS.toNanos(maxWait); final int maxWaitThreadCount = getMaxWaitThreadCount(); lock.lockInterruptibly(); } catch (InterruptedException e) { connectErrorCount.incrementAndGet(); throw e; } finally { lock.unlock();
@Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { boolean result = false; long startTime = System.nanoTime(); ReentrantLock compileLock = SessionState.get().getCompileLock(); try { result = compileLock.tryLock(time, unit) && globalCompileQuotas.tryAcquire( getRemainingTime(startTime, unit.toNanos(time)), TimeUnit.NANOSECONDS); } finally { if (!result && compileLock.isHeldByCurrentThread()) { compileLock.unlock(); } } return result; }
public E pollLast(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ((x = unlinkLast()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }
@Override public boolean waitForAvailable(long timeoutMillis) { boolean available = isAvailable(); if (available) { return true; } long remains = TimeUnit.MILLISECONDS.toNanos(timeoutMillis); final ReentrantLock _look = lock; _look.lock(); try { // avoid "spurious wakeup" occurs while (!(available = isAvailable())) { signalNeeded = 1; // set signal needed to true if ((remains = notifyCondition.awaitNanos(remains)) <= 0) { break; } } } catch (InterruptedException e) { ThrowUtil.throwException(e); } finally { _look.unlock(); } return available; }
@VisibleForTesting public void resume() throws InterruptedException { pauseLock.lockInterruptibly(); try { pauseRequested = false; shouldResume.signalAll(); long nanos = TimeUnit.SECONDS.toNanos(5); while (isPaused()) { if (nanos <= 0L) { throw new RuntimeException("Resume command was not accepted within 5 seconds"); } nanos = shouldResume.awaitNanos(nanos); } } finally { pauseLock.unlock(); } }
final long nanos = TimeUnit.MILLISECONDS.toNanos(maxWait); final int maxWaitThreadCount = this.maxWaitThreadCount; lock.unlock(); lock.lockInterruptibly(); } catch (InterruptedException e) { connectErrorCountUpdater.incrementAndGet(this); throw e; } finally { lock.unlock();
public E pollFirst(long timeout, TimeUnit unit) throws InterruptedException { long nanos = unit.toNanos(timeout); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { E x; while ((x = unlinkFirst()) == null) { if (nanos <= 0) return null; nanos = notEmpty.awaitNanos(nanos); } return x; } finally { lock.unlock(); } }
@Override public boolean waitForAvailable(long timeoutMillis) { boolean available = isAvailable(); if (available) { return true; } long remains = TimeUnit.MILLISECONDS.toNanos(timeoutMillis); final ReentrantLock _look = lock; _look.lock(); try { // avoid "spurious wakeup" occurs while (!(available = isAvailable())) { signalNeeded = 1; // set signal needed to true if ((remains = notifyCondition.awaitNanos(remains)) <= 0) { break; } } } catch (InterruptedException e) { ThrowUtil.throwException(e); } finally { _look.unlock(); } return available; }
@Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long nanoTimeWait = unit.toNanos(timeout); _unfinishedLock.lock(); try { while (!_unfinishedFutures.isEmpty()) { if ((nanoTimeWait = _unfinishedCondition.awaitNanos( nanoTimeWait)) <= 0) { return false; } } } finally { _unfinishedLock.unlock(); } return true; }
private List<T> pollObjects(int elementNum, long timeoutMs) throws InterruptedException { long nanos = TIME_UNIT.toNanos(timeoutMs); final List<T> list = new ArrayList<>(elementNum); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (objects.size() < elementNum) { if (nanos <= 0) { return Collections.emptyList(); } nanos = notEnough.awaitNanos(nanos); } for (int i = 0; i < elementNum; i++) { list.add(objects.pop()); } return list; } finally { lock.unlock(); } }
@Override public boolean waitForAvailable(long timeoutMillis) { if (connector.isDirectoryAvailable(directory)) { return true; } long remains = TimeUnit.MILLISECONDS.toNanos(timeoutMillis); boolean available = false; final ReentrantLock _look = lock; _look.lock(); try { signalNeeded.set(true); // avoid "spurious wakeup" occurs while (!(available = connector.isDirectoryAvailable(directory))) { if ((remains = notifyCondition.awaitNanos(remains)) <= 0) { break; } } } catch (InterruptedException e) { ThrowUtil.throwException(e); } finally { _look.unlock(); } return available || connector.isDirectoryAvailable(directory); } };
/** * Wait for the condition to become something else as {@link State#RequestSent} or {@link State#Initial}. * {@link #reportSuccess()}, {@link #reportFailure()} and {@link #reportFailure(Exception)} will either set this * synchronization point to {@link State#Success} or {@link State#Failure}. If none of them is set after the * connections reply timeout, this method will set the state of {@link State#NoResponse}. * @throws InterruptedException */ private void waitForConditionOrTimeout() throws InterruptedException { long remainingWait = TimeUnit.MILLISECONDS.toNanos(connection.getReplyTimeout()); while (state == State.RequestSent || state == State.Initial) { if (remainingWait <= 0) { state = State.NoResponse; break; } remainingWait = condition.awaitNanos(remainingWait); } }