本文整理匯總了Java中java.util.concurrent.locks.StampedLock.unlockRead方法的典型用法代碼示例。如果您正苦於以下問題:Java StampedLock.unlockRead方法的具體用法?Java StampedLock.unlockRead怎麽用?Java StampedLock.unlockRead使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.locks.StampedLock
的用法示例。
在下文中一共展示了StampedLock.unlockRead方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: interruptibleReader
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
static Reader interruptibleReader(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReader") { public void run() {
if (gate != null ) toTheStartingGate(gate);
try {
if (timeout < 0)
stamp(sl.readLockInterruptibly());
else
stamp(sl.tryReadLock(timeout, unit));
check(sl.validate(stamp()));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
示例2: 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);
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: 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);
}
示例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: testReadLockInterruptibly
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* readLockInterruptibly succeeds if lock free else is interruptible
*/
public void testReadLockInterruptibly() throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s;
s = lock.readLockInterruptibly();
lock.unlockRead(s);
s = lock.writeLockInterruptibly();
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
running.countDown();
lock.readLockInterruptibly();
}});
running.await();
waitForThreadToEnterWaitState(t, 100);
t.interrupt();
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例12: reader
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
static Reader reader(final StampedLock sl, final Phaser gate) {
return new Reader() { public void run() {
if (gate != null ) toTheStartingGate(gate);
stamp(sl.readLock());
try {
check(sl.validate(stamp()));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} finally { sl.unlockRead(stamp()); } }};
}
示例13: releaseReadLock
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* Releases read lock, checking isReadLocked before and after
*/
void releaseReadLock(StampedLock lock, long stamp) {
assertTrue(lock.isReadLocked());
assertValid(lock, stamp);
lock.unlockRead(stamp);
assertFalse(lock.isReadLocked());
assertTrue(lock.validate(stamp));
}
示例14: 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);
}
示例15: 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);
}