private void readLock() { try { if (!lock.readLock().tryLock(10, TimeUnit.SECONDS)) { throw new RuntimeException("Couldn't acquire READ lock!"); } } catch (InterruptedException e) { throw new CancellationException(); } }
/** * Best-effort attempt to remove an argument from a batch. This may get invoked when a cancellation occurs somewhere downstream. * This method finds the argument in the batch, and removes it. * * @param arg argument to remove from batch */ /* package-private */ void remove(RequestArgumentType arg) { if (batchStarted.get()) { //nothing we can do return; } if (batchLock.readLock().tryLock()) { try { /* double-check now that we have the lock - if the batch is started, deleting is useless */ if (batchStarted.get()) { return; } argumentMap.remove(arg); } finally { batchLock.readLock().unlock(); } } }
/** * Enters "busy" state. * * @return {@code true} if entered to busy state. */ public boolean enterBusy() { return !lock.writeLock().isHeldByCurrentThread() && lock.readLock().tryLock(); }
/** * Tests {@link LockResource} with {@link ReentrantReadWriteLock}. */ @Test public void reentrantReadWriteLock() { ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); try (LockResource r1 = new LockResource(lock.readLock())) { try (LockResource r2 = new LockResource(lock.readLock())) { assertEquals(lock.getReadHoldCount(), 2); assertTrue(lock.readLock().tryLock()); lock.readLock().unlock(); } } assertEquals(lock.getReadHoldCount(), 0); try (LockResource r1 = new LockResource(lock.writeLock())) { try (LockResource r2 = new LockResource(lock.readLock())) { assertTrue(lock.isWriteLockedByCurrentThread()); assertEquals(lock.getReadHoldCount(), 1); } } assertFalse(lock.isWriteLockedByCurrentThread()); assertEquals(lock.getReadHoldCount(), 0); try (LockResource r = new LockResource(lock.readLock())) { assertFalse(lock.writeLock().tryLock()); } } }
try while ( !newTransactionsLock.readLock().tryLock( 1, TimeUnit.SECONDS ) )
public boolean sharedLock() { return updatesLock.readLock().tryLock(); }
/** * Delegate the call to the internal {@link java.util.concurrent.locks.ReentrantReadWriteLock} instance * and then on successful acquisition of lock, increment the read lock count held by the thread */ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { if (delegate.readLock().tryLock(time, unit)) { incReadLockCount(); return true; } return false; }
@Override public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { aboutToAcquire(readWriteLock); try { return super.tryLock(timeout, unit); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock() { aboutToAcquire(readWriteLock); try { return super.tryLock(); } finally { lockStateChanged(readWriteLock); } }
/** * Delegate the call to the internal {@link java.util.concurrent.locks.ReentrantReadWriteLock} instance * and then on successful acquisition of lock, increment the read lock count held by the thread */ @Override public boolean tryLock() { if (delegate.readLock().tryLock()) { incReadLockCount(); return true; } return false; }
/** * Enters busy state. * * @return {@code true} if entered to busy state. */ private boolean enterBusy() { return opsLock.readLock().tryLock(); }
@Override public boolean tryLock() { aboutToAcquire(readWriteLock); try { return super.tryLock(); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { aboutToAcquire(readWriteLock); try { return super.tryLock(timeout, unit); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { aboutToAcquire(readWriteLock); try { return super.tryLock(timeout, unit); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock() { aboutToAcquire(readWriteLock); try { return super.tryLock(); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { aboutToAcquire(readWriteLock); try { return super.tryLock(timeout, unit); } finally { lockStateChanged(readWriteLock); } }
@Override public boolean tryLock() { aboutToAcquire(readWriteLock); try { return super.tryLock(); } finally { lockStateChanged(readWriteLock); } }
public void testReentrantReadLock_tryLock() throws Exception { LockingThread thread = new LockingThread(readLockA); thread.start(); thread.waitUntilHoldingLock(); assertFalse(writeLockA.tryLock()); assertTrue(readLockA.tryLock()); readLockA.unlock(); thread.releaseLockAndFinish(); assertTrue(writeLockA.tryLock()); assertTrue(readLockA.tryLock()); }
public void testReentrantWriteLock_tryLock() throws Exception { LockingThread thread = new LockingThread(writeLockA); thread.start(); thread.waitUntilHoldingLock(); assertFalse(writeLockA.tryLock()); assertFalse(readLockA.tryLock()); thread.releaseLockAndFinish(); assertTrue(writeLockA.tryLock()); assertTrue(readLockA.tryLock()); }