Refine search
@Override public Lock get() { return new ReentrantLock(false); } });
/** * Enters this monitor. Blocks at most the given time, and may be interrupted. * * @return whether the monitor was entered * @throws InterruptedException if interrupted while waiting */ @SuppressWarnings("GoodTime") // should accept a java.time.Duration public boolean enterInterruptibly(long time, TimeUnit unit) throws InterruptedException { return lock.tryLock(time, unit); }
public DruidAbstractDataSource(boolean lockFair){ lock = new ReentrantLock(lockFair); notEmpty = lock.newCondition(); empty = lock.newCondition(); }
AtomicBoolean txnFail = new AtomicBoolean(false); AtomicInteger callbacksReceived = new AtomicInteger(0); AtomicInteger callbacksExpected = new AtomicInteger(0); final Lock lock = new ReentrantLock(); final Condition condition = lock.newCondition(); if (incrementBuffer != null) { incrementBuffer.clear(); List<PutRequest> actions = serializer.getActions(); List<AtomicIncrementRequest> increments = serializer.getIncrements(); callbacksExpected.addAndGet(actions.size()); if (!batchIncrements) { callbacksExpected.addAndGet(increments.size()); lock.unlock();
private Lock lock = new ReentrantLock(); private Condition notFull = lock.newCondition(); private Condition notEmpty = lock.newCondition(); lock.lock(); try { while(queue.size() == capacity) { notFull.await(); notEmpty.signal(); } finally { lock.unlock(); try { while(queue.isEmpty()) { notEmpty.await();
@Override public void onEvent(Event event) throws Throwable { final BeforeEvent bEvent = (BeforeEvent) event; printer.println(String.format( "%s.%s will be delay %s(ms) on %s", bEvent.javaClassName, bEvent.javaMethodName, delayMs, Thread.currentThread().getName() )); delayLock.lock(); try { // 如果已经结束,则放弃本次请求 if (isFinishRef.get()) { return; } delayCondition.await(delayMs, TimeUnit.MILLISECONDS); } finally { delayLock.unlock(); } } }, BEFORE);
public void execute(final Runnable runnable) { if (threadAlive.get() >= threadNum) { try { reentrantLock.lock(); while (threadAlive.get() >= threadNum) { try { condition.await(); } catch (InterruptedException e) { reentrantLock.unlock(); threadAlive.incrementAndGet(); executorService.execute(new Runnable() { @Override
@Override public void run() { try { lock.lock(); try { while (!started.get()) { startCondition.await(); } } finally { lock.unlock(); } pruner.prune(); lock.lock(); try { ended.set(true); endCondition.signal(); } finally { lock.unlock(); } } catch (SerDeException | IOException | InterruptedException | HiveException e) { inError.set(true); } } }
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); int c = -1; final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } opQueue(new Node<E>(e)); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return true; }
public boolean offer(E e) { if (e == null) throw new NullPointerException(); final AtomicInteger count = this.count; if (count.get() == capacity) return false; int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; putLock.lock(); try { if (count.get() < capacity) { opQueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return c >= 0; }
public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); // Note: convention in all put/take/etc is to preset local var // holding count negative to indicate failure unless set. int c = -1; Node<E> node = new Node<E>(e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { notFull.await(); } opQueue(node); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); }
public E poll() { final AtomicInteger count = this.count; if (count.get() == 0) return null; E x = null; int c = -1; final ReentrantLock takeLock = this.takeLock; takeLock.lock(); try { if (count.get() > 0) { x = opQueue(null); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
@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(); } }
private void waitForAsyncReadComplete() throws IOException { stateChangeLock.lock(); isWaiting.set(true); try { // There is only one reader, and one writer, so the writer should signal only once, // but a while loop checking the wake up condition is still needed to avoid spurious wakeups. while (readInProgress) { asyncReadComplete.await(); } } catch (InterruptedException e) { InterruptedIOException iio = new InterruptedIOException(e.getMessage()); iio.initCause(e); throw iio; } finally { isWaiting.set(false); stateChangeLock.unlock(); } checkReadException(); }
public E take() throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = this.takeLock; takeLock.lockInterruptibly(); try { while (count.get() == 0) { notEmpty.await(); } x = opQueue(null); c = count.getAndDecrement(); if (c > 1) notEmpty.signal(); } finally { takeLock.unlock(); } if (c == capacity) signalNotFull(); return x; }
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(); } }
/** * Usually this method is called by an external component that watches * a directory to restart Ninja's dev mode. * * The restart will be executed with a delay. If a bunch of files are * changed at the same time only one restart is performed. * */ public void trigger() { // signal for a restart this.restartLock.lock(); try { // accumulate restart triggers (e.g. # of files changed) accumulatedTriggerCount.incrementAndGet(); this.restartRequested.signal(); } finally { this.restartLock.unlock(); } }