本文整理汇总了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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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());
}
示例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());
}
示例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);
}
示例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);
}
示例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);
}
示例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"));
}
示例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);
}