Java Code Examples for java.util.concurrent.locks.ReentrantLock#tryLock()
The following examples show how to use
java.util.concurrent.locks.ReentrantLock#tryLock() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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(); } } }
Example 2
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * Enters this monitor. Blocks at most the given time. * * @return whether the monitor was entered */ public boolean enter(long time, TimeUnit unit) { final long timeoutNanos = toSafeNanos(time, unit); final ReentrantLock lock = this.lock; if (!fair && lock.tryLock()) { return true; } boolean interrupted = Thread.interrupted(); try { final long startTime = System.nanoTime(); for (long remainingNanos = timeoutNanos; ; ) { try { return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS); } catch (InterruptedException interrupt) { interrupted = true; remainingNanos = remainingNanos(startTime, timeoutNanos); } } } finally { if (interrupted) { Thread.currentThread().interrupt(); } } }
Example 3
Source File: ForkJoinTask.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * 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(); } } }
Example 4
Source File: LockPerfMain.java From Mycat2 with GNU General Public License v3.0 | 6 votes |
public void tReentrantLock() { System.currentTimeMillis(); ReentrantLock lock = new ReentrantLock(); long t1 = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { if (lock.tryLock()) { try { // ... } finally { lock.unlock(); } } } long t2 = System.currentTimeMillis(); System.out.println("take time:" + (t2 - t1) + " ms."); }
Example 5
Source File: TimeoutLockLoops.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public final void run() { try { barrier.await(); int sum = v; int x = 17; final ReentrantLock lock = this.lock; while (lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) { try { v = x = LoopHelpers.compute1(v); } finally { lock.unlock(); } sum += LoopHelpers.compute2(x); } barrier.await(); result += sum; } catch (Throwable ex) { fail = ex; throw new RuntimeException(ex); } }
Example 6
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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(); } } }
Example 7
Source File: CopyOnWriteGroupList.java From Jupiter with Apache License 2.0 | 6 votes |
public final boolean setWeightArray(JChannelGroup[] snapshot, String directory, Object weightArray) { if (weightArray == null || snapshot != tabAt0(array)) { return false; } final ReentrantLock lock = this.lock; boolean locked = lock.tryLock(); if (locked) { // give up if there is competition try { if (snapshot != tabAt0(array)) { return false; } setWeightArray(directory, weightArray); return true; } finally { lock.unlock(); } } return false; }
Example 8
Source File: LockPerfMain.java From tddl5 with Apache License 2.0 | 6 votes |
public void tReentrantLock() { System.currentTimeMillis(); ReentrantLock lock = new ReentrantLock(); long t1 = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { if (lock.tryLock()) try { // ... } finally { lock.unlock(); } } long t2 = System.currentTimeMillis(); System.out.println("take time:" + (t2 - t1) + " ms."); }
Example 9
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 6 votes |
/** * 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(); } } }
Example 10
Source File: ForkJoinTask.java From streamsupport with GNU General Public License v2.0 | 5 votes |
/** * If lock is available, polls stale refs and removes them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
Example 11
Source File: ForkJoinTask.java From a-foundation with Apache License 2.0 | 5 votes |
/** * 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(); } } }
Example 12
Source File: ForkJoinTask.java From j360-dubbo-app-all with Apache License 2.0 | 5 votes |
/** * 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(); } } }
Example 13
Source File: ForkJoinTask.java From jdk1.8-source-analysis with Apache License 2.0 | 5 votes |
/** * 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(); } } }
Example 14
Source File: ForkJoinTask.java From j2objc with Apache License 2.0 | 5 votes |
/** * If lock is available, polls stale refs and removes them. * Called from ForkJoinPool when pools become quiescent. */ static final void helpExpungeStaleExceptions() { final ReentrantLock lock = exceptionTableLock; if (lock.tryLock()) { try { expungeStaleExceptions(); } finally { lock.unlock(); } } }
Example 15
Source File: ForkJoinTask.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
/** * 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(); } } }
Example 16
Source File: LocalLockServiceImpl.java From limiter with MIT License | 4 votes |
@Override public boolean tryLock(String source) { ReentrantLock reentrantLock = initAndGetLock(source); return reentrantLock.tryLock(); }
Example 17
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both * the time to acquire the lock and the time to 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 enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException { final long timeoutNanos = toSafeNanos(time, unit); if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; boolean reentrant = lock.isHeldByCurrentThread(); long startTime = 0L; locked: { if (!fair) { // Check interrupt status to get behavior consistent with fair case. if (Thread.interrupted()) { throw new InterruptedException(); } if (lock.tryLock()) { break locked; } } startTime = initNanoTime(timeoutNanos); if (!lock.tryLock(time, unit)) { return false; } } boolean satisfied = false; boolean threw = true; try { satisfied = guard.isSatisfied() || awaitNanos( guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant); threw = false; return satisfied; } finally { if (!satisfied) { try { // Don't need to signal if timed out, but do if interrupted if (threw && !reentrant) { signalNextWaiter(); } } finally { lock.unlock(); } } } }
Example 18
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both * the time to acquire the lock and the time to 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 enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException { final long timeoutNanos = toSafeNanos(time, unit); if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; boolean reentrant = lock.isHeldByCurrentThread(); long startTime = 0L; locked: { if (!fair) { // Check interrupt status to get behavior consistent with fair case. if (Thread.interrupted()) { throw new InterruptedException(); } if (lock.tryLock()) { break locked; } } startTime = initNanoTime(timeoutNanos); if (!lock.tryLock(time, unit)) { return false; } } boolean satisfied = false; boolean threw = true; try { satisfied = guard.isSatisfied() || awaitNanos(guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant); threw = false; return satisfied; } finally { if (!satisfied) { try { // Don't need to signal if timed out, but do if interrupted if (threw && !reentrant) { signalNextWaiter(); } } finally { lock.unlock(); } } } }
Example 19
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both * the time to acquire the lock and the time to 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 enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException { final long timeoutNanos = toSafeNanos(time, unit); if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; boolean reentrant = lock.isHeldByCurrentThread(); long startTime = 0L; locked: { if (!fair) { // Check interrupt status to get behavior consistent with fair case. if (Thread.interrupted()) { throw new InterruptedException(); } if (lock.tryLock()) { break locked; } } startTime = initNanoTime(timeoutNanos); if (!lock.tryLock(time, unit)) { return false; } } boolean satisfied = false; boolean threw = true; try { satisfied = guard.isSatisfied() || awaitNanos(guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant); threw = false; return satisfied; } finally { if (!satisfied) { try { // Don't need to signal if timed out, but do if interrupted if (threw && !reentrant) { signalNextWaiter(); } } finally { lock.unlock(); } } } }
Example 20
Source File: Monitor.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both * the time to acquire the lock and the time to 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 enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException { final long timeoutNanos = toSafeNanos(time, unit); if (guard.monitor != this) { throw new IllegalMonitorStateException(); } final ReentrantLock lock = this.lock; boolean reentrant = lock.isHeldByCurrentThread(); long startTime = 0L; locked: { if (!fair) { // Check interrupt status to get behavior consistent with fair case. if (Thread.interrupted()) { throw new InterruptedException(); } if (lock.tryLock()) { break locked; } } startTime = initNanoTime(timeoutNanos); if (!lock.tryLock(time, unit)) { return false; } } boolean satisfied = false; boolean threw = true; try { satisfied = guard.isSatisfied() || awaitNanos(guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant); threw = false; return satisfied; } finally { if (!satisfied) { try { // Don't need to signal if timed out, but do if interrupted if (threw && !reentrant) { signalNextWaiter(); } } finally { lock.unlock(); } } } }