本文整理匯總了Java中java.util.concurrent.locks.StampedLock.asReadLock方法的典型用法代碼示例。如果您正苦於以下問題:Java StampedLock.asReadLock方法的具體用法?Java StampedLock.asReadLock怎麽用?Java StampedLock.asReadLock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.locks.StampedLock
的用法示例。
在下文中一共展示了StampedLock.asReadLock方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: interruptibleReaderView
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
static Reader interruptibleReaderView(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReaderView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
final Lock rl = sl.asReadLock();
try {
if (timeout < 0)
rl.lockInterruptibly();
else
rl.tryLock(timeout, unit);
stamp(1L); // got the lock
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) rl.unlock(); } }};
}
示例2: testAsReadLock
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* asReadLock can be locked and unlocked
*/
public void testAsReadLock() throws Throwable {
StampedLock sl = new StampedLock();
Lock lock = sl.asReadLock();
for (Action locker : lockLockers(lock)) {
locker.run();
assertTrue(sl.isReadLocked());
assertFalse(sl.isWriteLocked());
assertEquals(1, sl.getReadLockCount());
locker.run();
assertTrue(sl.isReadLocked());
assertEquals(2, sl.getReadLockCount());
lock.unlock();
lock.unlock();
assertUnlocked(sl);
}
}
示例3: readerView
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
static Reader readerView(final StampedLock sl, final Phaser gate) {
return new Reader("ReaderView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
final Lock rl = sl.asReadLock();
rl.lock();
try {
stamp(1L); // got the lock
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} finally { rl.unlock(); } }};
}
示例4: testAsReadLock
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* asReadLock can be locked and unlocked
*/
public void testAsReadLock() {
StampedLock sl = new StampedLock();
Lock lock = sl.asReadLock();
lock.lock();
lock.unlock();
assertTrue(lock.tryLock());
}