private void writeLock() { try { if (!lock.writeLock().tryLock(10, TimeUnit.SECONDS)) { throw new RuntimeException("Couldn't acquire WRITE lock!"); } } catch (InterruptedException e) { throw new CancellationException(); } }
boolean locked = dagLock.writeLock().tryLock(); if (!locked) {
/** {@inheritDoc} */ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { int i = 0; long end = unit.toNanos(time) + System.nanoTime(); try { for (; i < locks.length && System.nanoTime() < end; i++) { if (!locks[i].writeLock().tryLock(time, unit)) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
boolean succeed = lock.writeLock().tryLock(timeoutForWriteLock, TimeUnit.SECONDS); if (!succeed) { throw new IOException("Failed to get write lock when closing region");
queryInfo.getQueryIdentifier()); boolean locked = dagLock.writeLock().tryLock(); if (!locked) {
@Override public void run() { LOG.debug("Running {}", this); if (monitorStarted.get()) { try { // If we can't get the lock it means another // write beat us into the // send and we don't need to heart beat now. if (sendLock.writeLock().tryLock()) { KeepAliveInfo info = new KeepAliveInfo(); info.setResponseRequired(keepAliveResponseRequired); doOnewaySend(info); } } catch (IOException e) { onException(e); } finally { if (sendLock.writeLock().isHeldByCurrentThread()) { sendLock.writeLock().unlock(); } } } }
/** * 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()); } } }
/** {@inheritDoc} */ @Override public boolean tryLock() { int i = 0; try { for (; i < locks.length; i++) { if (!locks[i].writeLock().tryLock()) break; } } finally { if (0 < i && i < locks.length) unlock0(i - 1); } return i == locks.length; }
/** * Ensures that the current thread doesn't hold any read locks. If * the thread holds any read locks, this method throws a {@link javax.ejb.IllegalLoopbackException}. * If no read locks are held, then this method delegates the call to the * internal delegate {@link java.util.concurrent.locks.ReentrantReadWriteLock} */ @Override public boolean tryLock() { checkLoopback(); return delegate.writeLock().tryLock(); }
@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); } }
/** * Ensures that the current thread doesn't hold any read locks. If * the thread holds any read locks, this method throws a {@link javax.ejb.IllegalLoopbackException}. * If no read locks are held, then this method delegates the call to the * internal delegate {@link java.util.concurrent.locks.ReentrantReadWriteLock} */ @Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException { checkLoopback(); return delegate.writeLock().tryLock(time, unit); }
@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); } }
@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()); }
@Override public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException { aboutToAcquire(readWriteLock); try { return super.tryLock(timeout, unit); } finally { lockStateChanged(readWriteLock); } }
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()); }