Java Code Examples for java.util.concurrent.locks.StampedLock#tryWriteLock()

The following examples show how to use java.util.concurrent.locks.StampedLock#tryWriteLock() . 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: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * interruptible operations throw InterruptedException when write locked and interrupted
 */
public void testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted() {
    final StampedLock lock = new StampedLock();
    long s = lock.writeLock();

    Action[] interruptibleLockBlockingActions = {
        () -> lock.writeLockInterruptibly(),
        () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
        () -> lock.readLockInterruptibly(),
        () -> lock.tryReadLock(Long.MAX_VALUE, DAYS),
        () -> lock.asWriteLock().lockInterruptibly(),
        () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
        () -> lock.asReadLock().lockInterruptibly(),
        () -> lock.asReadLock().tryLock(Long.MAX_VALUE, DAYS),
    };
    shuffle(interruptibleLockBlockingActions);

    assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
}
 
Example 2
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * interruptible operations throw InterruptedException when read locked and interrupted
 */
public void testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();

    Action[] interruptibleLockBlockingActions = {
        () -> lock.writeLockInterruptibly(),
        () -> lock.tryWriteLock(Long.MAX_VALUE, DAYS),
        () -> lock.asWriteLock().lockInterruptibly(),
        () -> lock.asWriteLock().tryLock(Long.MAX_VALUE, DAYS),
    };
    shuffle(interruptibleLockBlockingActions);

    assertThrowInterruptedExceptionWhenInterrupted(interruptibleLockBlockingActions);
}
 
Example 3
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * tryWriteLock on an unlocked lock succeeds
 */
public void testTryWriteLock() {
    final StampedLock lock = new StampedLock();
    long s = lock.tryWriteLock();
    assertTrue(s != 0L);
    assertTrue(lock.isWriteLocked());
    assertEquals(0L, lock.tryWriteLock());
    releaseWriteLock(lock, s);
}
 
Example 4
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static long tryWriteLockUninterrupted(StampedLock sl, long time, TimeUnit unit) {
    try { return sl.tryWriteLock(time, unit); }
    catch (InterruptedException ex) { throw new AssertionError(ex); }
}