@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 T takeObject() throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (objects.isEmpty()) { notEnough.await(); } return objects.pop(); } finally { lock.unlock(); } }
/** * Invokes the supplied {@link Runnable} if the {@link Queue} lock was obtained without blocking. * * @param runnable the operation to perform. * @return {@code true} if the lock was available and the operation was performed. * @since 1.618 */ protected boolean _tryWithLock(Runnable runnable) { if (lock.tryLock()) { try { runnable.run(); } finally { lock.unlock(); } return true; } else { return false; } }
public Events<Event> get(Position start, int batchSize) throws InterruptedException, CanalStoreException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (!checkUnGetSlotAt((LogPosition) start, batchSize)) notEmpty.await(); } catch (InterruptedException ie) { notEmpty.signal(); // propagate to non-interrupted thread throw ie; } return doGet(start, batchSize); } finally { lock.unlock(); } }
@Override public AsyncResult poll() throws InterruptedException { lock.lockInterruptibly(); try { while (completedQueue.isEmpty()) { hasCompletedEntries.await(); } numberEntries--; notFull.signalAll(); LOG.debug("Polled element from unordered stream element queue. New filling degree " + "({}/{}).", numberEntries, capacity); return completedQueue.poll(); } finally { lock.unlock(); } }
public void destory() { try { lock.lockInterruptibly(); timer.cancel(); binlogs.clear(); nextCondition.signalAll();// 唤醒线程,通知退出 } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { lock.unlock(); } }
/** * Enters this monitor if the guard is satisfied. Blocks indefinitely acquiring the lock, but does * not wait for the guard to be satisfied, and may be interrupted. * * @return whether the monitor was entered, which guarantees that the guard is now satisfied * @throws InterruptedException if interrupted while waiting */ public boolean enterIfInterruptibly(Guard guard) throws InterruptedException { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; lock.lockInterruptibly(); boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { 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 <T> void put(StreamElementQueueEntry<T> streamElementQueueEntry) throws InterruptedException { lock.lockInterruptibly(); try { while (queue.size() >= capacity) { notFull.await(); } addEntry(streamElementQueueEntry); } finally { lock.unlock(); } }