当前位置: 首页>>代码示例>>Java>>正文


Java StampedLock.readLock方法代码示例

本文整理汇总了Java中java.util.concurrent.locks.StampedLock.readLock方法的典型用法代码示例。如果您正苦于以下问题:Java StampedLock.readLock方法的具体用法?Java StampedLock.readLock怎么用?Java StampedLock.readLock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.locks.StampedLock的用法示例。


在下文中一共展示了StampedLock.readLock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testMultipleReadLocks

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * Multiple threads can hold a read lock when not write-locked
 */
public void testMultipleReadLocks() {
    final StampedLock lock = new StampedLock();
    final long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            long s2 = lock.tryReadLock();
            assertValid(lock, s2);
            lock.unlockRead(s2);
            long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
            assertValid(lock, s3);
            lock.unlockRead(s3);
            long s4 = lock.readLock();
            assertValid(lock, s4);
            lock.unlockRead(s4);
            lock.asReadLock().lock();
            lock.asReadLock().unlock();
            lock.asReadLock().lockInterruptibly();
            lock.asReadLock().unlock();
            lock.asReadLock().tryLock(Long.MIN_VALUE, DAYS);
            lock.asReadLock().unlock();
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:StampedLockTest.java

示例2: testWriteAfterReadLock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * writeLock() succeeds only after a reading thread unlocks
 */
public void testWriteAfterReadLock() throws InterruptedException {
    final CountDownLatch aboutToLock = new CountDownLatch(1);
    final StampedLock lock = new StampedLock();
    long rs = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            aboutToLock.countDown();
            long s = lock.writeLock();
            assertTrue(lock.isWriteLocked());
            assertFalse(lock.isReadLocked());
            lock.unlockWrite(s);
        }});

    await(aboutToLock);
    assertThreadBlocks(t, Thread.State.WAITING);
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    lock.unlockRead(rs);
    awaitTermination(t);
    assertUnlocked(lock);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:StampedLockTest.java

示例3: testWriteAfterMultipleReadLocks

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * writeLock() succeeds only after reading threads unlock
 */
public void testWriteAfterMultipleReadLocks() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long rs = lock.readLock();
            lock.unlockRead(rs);
        }});

    awaitTermination(t1);

    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long ws = lock.writeLock();
            lock.unlockWrite(ws);
        }});

    assertTrue(lock.isReadLocked());
    assertFalse(lock.isWriteLocked());
    lock.unlockRead(s);
    awaitTermination(t2);
    assertUnlocked(lock);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:StampedLockTest.java

示例4: testReadAfterWriteLock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * readLock() succeed only after a writing thread unlocks
 */
public void testReadAfterWriteLock() {
    final StampedLock lock = new StampedLock();
    final CountDownLatch threadsStarted = new CountDownLatch(2);
    final long s = lock.writeLock();
    final Runnable acquireReleaseReadLock = new CheckedRunnable() {
        public void realRun() {
            threadsStarted.countDown();
            long rs = lock.readLock();
            assertTrue(lock.isReadLocked());
            assertFalse(lock.isWriteLocked());
            lock.unlockRead(rs);
        }};
    Thread t1 = newStartedThread(acquireReleaseReadLock);
    Thread t2 = newStartedThread(acquireReleaseReadLock);

    await(threadsStarted);
    assertThreadBlocks(t1, Thread.State.WAITING);
    assertThreadBlocks(t2, Thread.State.WAITING);
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    releaseWriteLock(lock, s);
    awaitTermination(t1);
    awaitTermination(t2);
    assertUnlocked(lock);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:StampedLockTest.java

示例5: testLock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * write-locking and read-locking an unlocked lock succeed
 */
public void testLock() {
    StampedLock lock = new StampedLock();
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long s = lock.writeLock();
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    lock.unlockWrite(s);
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long rs = lock.readLock();
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 1);
    lock.unlockRead(rs);
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:26,代码来源:StampedLockTest.java

示例6: testUnlock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * unlock releases either a read or write lock
 */
public void testUnlock() {
    StampedLock lock = new StampedLock();
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long s = lock.writeLock();
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    lock.unlock(s);
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long rs = lock.readLock();
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 1);
    lock.unlock(rs);
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:26,代码来源:StampedLockTest.java

示例7: testValidate

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * A stamp obtained from a successful lock operation validates
 */
public void testValidate() throws InterruptedException {
    StampedLock lock = new StampedLock();
    long s = lock.writeLock();
    assertTrue(lock.validate(s));
    lock.unlockWrite(s);
    s = lock.readLock();
    assertTrue(lock.validate(s));
    lock.unlockRead(s);
    assertTrue((s = lock.tryWriteLock()) != 0L);
    assertTrue(lock.validate(s));
    lock.unlockWrite(s);
    assertTrue((s = lock.tryReadLock()) != 0L);
    assertTrue(lock.validate(s));
    lock.unlockRead(s);
    assertTrue((s = lock.tryWriteLock(100L, MILLISECONDS)) != 0L);
    assertTrue(lock.validate(s));
    lock.unlockWrite(s);
    assertTrue((s = lock.tryReadLock(100L, MILLISECONDS)) != 0L);
    assertTrue(lock.validate(s));
    lock.unlockRead(s);
    assertTrue((s = lock.tryOptimisticRead()) != 0L);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:26,代码来源:StampedLockTest.java

示例8: testMultipleReadLocks

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * Multiple threads can hold a read lock when not write-locked
 */
public void testMultipleReadLocks() {
    final StampedLock lock = new StampedLock();
    final long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            long s2 = lock.tryReadLock();
            assertTrue(s2 != 0L);
            lock.unlockRead(s2);
            long s3 = lock.tryReadLock(LONG_DELAY_MS, MILLISECONDS);
            assertTrue(s3 != 0L);
            lock.unlockRead(s3);
            long s4 = lock.readLock();
            lock.unlockRead(s4);
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:22,代码来源:StampedLockTest.java

示例9: testWriteAfterReadLock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * A writelock succeeds only after a reading thread unlocks
 */
public void testWriteAfterReadLock() throws InterruptedException {
    final CountDownLatch running = new CountDownLatch(1);
    final StampedLock lock = new StampedLock();
    long rs = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            running.countDown();
            long s = lock.writeLock();
            lock.unlockWrite(s);
        }});

    running.await();
    waitForThreadToEnterWaitState(t, 100);
    assertFalse(lock.isWriteLocked());
    lock.unlockRead(rs);
    awaitTermination(t);
    assertFalse(lock.isWriteLocked());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:22,代码来源:StampedLockTest.java

示例10: testWriteAfterMultipleReadLocks

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * A writelock succeeds only after reading threads unlock
 */
public void testWriteAfterMultipleReadLocks() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long rs = lock.readLock();
            lock.unlockRead(rs);
        }});

    awaitTermination(t1);

    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long ws = lock.writeLock();
            lock.unlockWrite(ws);
        }});

    assertFalse(lock.isWriteLocked());
    lock.unlockRead(s);
    awaitTermination(t2);
    assertFalse(lock.isWriteLocked());
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:26,代码来源:StampedLockTest.java

示例11: testInterruptibleOperationsThrowInterruptedExceptionReadLockedInterrupted

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:StampedLockTest.java

示例12: testTryLockWhenReadLocked

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * tryReadLock succeeds if read locked but not write locked
 */
public void testTryLockWhenReadLocked() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            long rs = lock.tryReadLock();
            assertValid(lock, rs);
            lock.unlockRead(rs);
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:StampedLockTest.java

示例13: testTryWriteLockWhenReadLocked

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * tryWriteLock fails when read locked
 */
public void testTryWriteLockWhenReadLocked() {
    final StampedLock lock = new StampedLock();
    long s = lock.readLock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() {
            threadAssertEquals(0L, lock.tryWriteLock());
        }});

    awaitTermination(t);
    lock.unlockRead(s);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:StampedLockTest.java

示例14: testToString

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * toString indicates current lock state
 */
public void testToString() {
    StampedLock lock = new StampedLock();
    assertTrue(lock.toString().contains("Unlocked"));
    long s = lock.writeLock();
    assertTrue(lock.toString().contains("Write-locked"));
    lock.unlockWrite(s);
    s = lock.readLock();
    assertTrue(lock.toString().contains("Read-locks"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:StampedLockTest.java

示例15: testTryUnlock

import java.util.concurrent.locks.StampedLock; //导入方法依赖的package包/类
/**
 * tryUnlockRead/Write succeeds if locked in associated mode else
 * returns false
 */
public void testTryUnlock() {
    StampedLock lock = new StampedLock();
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long s = lock.writeLock();
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    assertFalse(lock.tryUnlockRead());
    assertTrue(lock.tryUnlockWrite());
    assertFalse(lock.tryUnlockWrite());
    assertFalse(lock.tryUnlockRead());
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
    long rs = lock.readLock();
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 1);
    assertFalse(lock.tryUnlockWrite());
    assertTrue(lock.tryUnlockRead());
    assertFalse(lock.tryUnlockRead());
    assertFalse(lock.tryUnlockWrite());
    assertFalse(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    assertEquals(lock.getReadLockCount(), 0);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:33,代码来源:StampedLockTest.java


注:本文中的java.util.concurrent.locks.StampedLock.readLock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。