本文整理匯總了Java中java.util.concurrent.locks.StampedLock.writeLock方法的典型用法代碼示例。如果您正苦於以下問題:Java StampedLock.writeLock方法的具體用法?Java StampedLock.writeLock怎麽用?Java StampedLock.writeLock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.locks.StampedLock
的用法示例。
在下文中一共展示了StampedLock.writeLock方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testInterruptibleOperationsThrowInterruptedExceptionWriteLockedInterrupted
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* 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);
}
示例2: 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);
}
示例3: 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);
}
示例4: 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);
}
示例5: 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);
}
示例6: testWriteLockInterruptibly_Interruptible
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* writeLockInterruptibly is interruptible
*/
public void testWriteLockInterruptibly_Interruptible()
throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
running.countDown();
lock.writeLockInterruptibly();
}});
running.await();
waitForThreadToEnterWaitState(t, 100);
t.interrupt();
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例7: testWriteTryLock_Interruptible
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* timed tryWriteLock is interruptible
*/
public void testWriteTryLock_Interruptible() throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
running.countDown();
lock.tryWriteLock(2 * LONG_DELAY_MS, MILLISECONDS);
}});
running.await();
waitForThreadToEnterWaitState(t, 100);
t.interrupt();
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例8: testReadLockInterruptibly_Interruptible
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* readLockInterruptibly is interruptible
*/
public void testReadLockInterruptibly_Interruptible()
throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
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);
}
示例9: testReadTryLock_Interruptible
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* timed tryReadLock is interruptible
*/
public void testReadTryLock_Interruptible() throws InterruptedException {
final CountDownLatch running = new CountDownLatch(1);
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedInterruptedRunnable() {
public void realRun() throws InterruptedException {
running.countDown();
lock.tryReadLock(2 * LONG_DELAY_MS, MILLISECONDS);
}});
running.await();
waitForThreadToEnterWaitState(t, 100);
t.interrupt();
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例10: testWriteTryLock_Timeout
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* timed tryWriteLock times out if locked
*/
public void testWriteTryLock_Timeout() {
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
long timeoutMillis = 10;
long ws = lock.tryWriteLock(timeoutMillis, MILLISECONDS);
assertEquals(ws, 0L);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}});
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例11: testReadTryLock_Timeout
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* timed tryReadLock times out if write-locked
*/
public void testReadTryLock_Timeout() {
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
long startTime = System.nanoTime();
long timeoutMillis = 10;
long rs = lock.tryReadLock(timeoutMillis, MILLISECONDS);
assertEquals(rs, 0L);
assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
}});
awaitTermination(t);
assertTrue(lock.isWriteLocked());
lock.unlockWrite(s);
}
示例12: testTryWriteLockWhenLocked
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* tryWriteLock fails if locked
*/
public void testTryWriteLockWhenLocked() {
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0L, lock.tryWriteLock());
}});
assertEquals(0L, lock.tryWriteLock());
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例13: testTryReadLockWhenLocked
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* tryReadLock fails if write-locked
*/
public void testTryReadLockWhenLocked() {
final StampedLock lock = new StampedLock();
long s = lock.writeLock();
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0L, lock.tryReadLock());
}});
assertEquals(0L, lock.tryReadLock());
awaitTermination(t);
releaseWriteLock(lock, s);
}
示例14: testSerialization
import java.util.concurrent.locks.StampedLock; //導入方法依賴的package包/類
/**
* A serialized lock deserializes as unlocked
*/
public void testSerialization() {
StampedLock lock = new StampedLock();
lock.writeLock();
StampedLock clone = serialClone(lock);
assertTrue(lock.isWriteLocked());
assertFalse(clone.isWriteLocked());
long s = clone.writeLock();
assertTrue(clone.isWriteLocked());
clone.unlockWrite(s);
assertFalse(clone.isWriteLocked());
}
示例15: 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"));
}