Refine search
/** * 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 void removeExpiredSessions(Instant now) { if (sessions.isEmpty()) { return; } if (this.lock.tryLock()) { try { Iterator<InMemoryWebSession> iterator = sessions.values().iterator(); while (iterator.hasNext()) { InMemoryWebSession session = iterator.next(); if (session.isExpired(now)) { iterator.remove(); session.invalidate(); } } } finally { this.checkTime = now.plus(CHECK_PERIOD, ChronoUnit.MILLIS); this.lock.unlock(); } } } }
/** * Enters this monitor if the guard is satisfied. Blocks at most the given time 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 */ @SuppressWarnings("GoodTime") // should accept a java.time.Duration public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit) throws InterruptedException { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock(time, unit)) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }
/** * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not * block acquiring the lock and does not wait for the guard to be satisfied. * * <p><b>Note:</b> This method disregards the fairness setting of this monitor. * * @return whether the monitor was entered, which guarantees that the guard is now satisfied */ public boolean tryEnterIf(Guard guard) { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock()) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }
private int allocateFast(int freeListIx, MemoryBuffer[] dest, long[] destHeaders, int destIx, int destCount, int allocSize) { if (data == null) return -1; // not allocated yet FreeList freeList = freeLists[freeListIx]; if (!freeList.lock.tryLock()) return destIx; try { return allocateFromFreeListUnderLock( freeList, freeListIx, dest, destHeaders, destIx, destCount, allocSize); } finally { freeList.lock.unlock(); } }
/** * Enters this monitor if the guard is satisfied. Blocks at most the given time 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 */ public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit) throws InterruptedException { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock(time, unit)) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
/** * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not * block acquiring the lock and does not wait for the guard to be satisfied. * * <p><b>Note:</b> This method disregards the fairness setting of this monitor. * * @return whether the monitor was entered, which guarantees that the guard is now satisfied */ public boolean tryEnterIf(Guard guard) { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock()) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }
/** * Try lock - Timed and polled lock acquisition * * @throws InterruptedException */ public void lockMyHearthWithTiming() throws InterruptedException { // Tries to acquire lock in the specified timeout if (!reentrantLock.tryLock(1l, TimeUnit.SECONDS)) { System.err.println("Failed to acquire the lock - it's already held."); } else { try { System.out.println("Simulating a blocking computation - forcing tryLock() to fail"); Thread.sleep(3000); } finally { reentrantLock.unlock(); } } }
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
/** * If lock is available, poll stale refs and remove them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
public void removeExpiredSessions(Instant now) { if (sessions.isEmpty()) { return; } if (this.lock.tryLock()) { try { Iterator<InMemoryWebSession> iterator = sessions.values().iterator(); while (iterator.hasNext()) { InMemoryWebSession session = iterator.next(); if (session.isExpired(now)) { iterator.remove(); session.invalidate(); } } } finally { this.checkTime = now.plus(CHECK_PERIOD, ChronoUnit.MILLIS); this.lock.unlock(); } } } }
/** * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not * block acquiring the lock and does not wait for the guard to be satisfied. * * <p><b>Note:</b> This method disregards the fairness setting of this monitor. * * @return whether the monitor was entered, which guarantees that the guard is now satisfied */ public boolean tryEnterIf(Guard guard) { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock()) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }
/** * Enters this monitor if the guard is satisfied. Blocks at most the given time 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 */ public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit) throws InterruptedException { if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; if (!lock.tryLock(time, unit)) { return false; } boolean satisfied = false; try { return satisfied = guard.isSatisfied(); } finally { if (!satisfied) { lock.unlock(); } } }