当前位置: 首页>>代码示例>>Java>>正文


Java StampedLock.unlockRead方法代码示例

本文整理汇总了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()); } }};
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:Basic.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:StampedLockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:StampedLockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:StampedLockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:StampedLockTest.java

示例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);
}
 
开发者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: 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);
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:23,代码来源:StampedLockTest.java

示例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()); } }};
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:Basic.java

示例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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:StampedLockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:StampedLockTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:StampedLockTest.java


注:本文中的java.util.concurrent.locks.StampedLock.unlockRead方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。