當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。