java.util.concurrent.locks.StampedLock#unlockRead()源码实例Demo

下面列出了java.util.concurrent.locks.StampedLock#unlockRead() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
}
 
源代码2 项目: sofa-jraft   文件: RegionRouteTable.java
/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<byte[]>> findRegionsByKeys(final List<byte[]> keys) {
    Requires.requireNonNull(keys, "keys");
    final Map<Region, List<byte[]>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final byte[] key : keys) {
            final Region region = findRegionByKeyWithoutLock(key);
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(key);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
 
源代码3 项目: sofa-jraft   文件: RegionRouteTable.java
/**
 * Returns the list of regions to which the keys belongs.
 */
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
    Requires.requireNonNull(kvEntries, "kvEntries");
    final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
    final StampedLock stampedLock = this.stampedLock;
    final long stamp = stampedLock.readLock();
    try {
        for (final KVEntry kvEntry : kvEntries) {
            final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
            regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
        }
        return regionMap;
    } finally {
        stampedLock.unlockRead(stamp);
    }
}
 
源代码4 项目: sofa-jraft   文件: RegionRouteTable.java
/**
 * Returns the startKey of next region.
 */
public byte[] findStartKeyOfNextRegion(final byte[] key) {
    Requires.requireNonNull(key, "key");
    final StampedLock stampedLock = this.stampedLock;
    long stamp = stampedLock.tryOptimisticRead();
    // get the least key strictly greater than the given key
    byte[] nextStartKey = this.rangeTable.higherKey(key);
    if (!stampedLock.validate(stamp)) {
        stamp = stampedLock.readLock();
        try {
            // get the least key strictly greater than the given key
            nextStartKey = this.rangeTable.higherKey(key);
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return nextStartKey;
}
 
源代码5 项目: dragonwell8_jdk   文件: Basic.java
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()); } }};
}
 
源代码6 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
    waitForThreadToEnterWaitState(t1);
    waitForThreadToEnterWaitState(t2);
    assertTrue(lock.isWriteLocked());
    assertFalse(lock.isReadLocked());
    releaseWriteLock(lock, s);
    awaitTermination(t1);
    awaitTermination(t2);
    assertUnlocked(lock);
}
 
源代码7 项目: jdk8u-jdk   文件: Basic.java
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()); } }};
}
 
源代码8 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
        }});

    aboutToLock.await();
    waitForThreadToEnterWaitState(t);
    assertFalse(lock.isWriteLocked());
    assertTrue(lock.isReadLocked());
    lock.unlockRead(rs);
    awaitTermination(t);
    assertUnlocked(lock);
}
 
源代码9 项目: native-obfuscator   文件: Basic.java
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()); } }};
}
 
源代码10 项目: hottub   文件: Basic.java
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()); } }};
}
 
源代码11 项目: Jupiter   文件: AbstractRegistryService.java
@Override
public Map<ServiceMeta, Integer> consumers() {
    Map<ServiceMeta, Integer> result = Maps.newHashMap();
    for (Map.Entry<RegisterMeta.ServiceMeta, RegisterValue> entry : registries.entrySet()) {
        RegisterValue value = entry.getValue();
        final StampedLock stampedLock = value.lock;
        long stamp = stampedLock.tryOptimisticRead();
        int optimisticVal = value.metaSet.size();
        if (stampedLock.validate(stamp)) {
            result.put(entry.getKey(), optimisticVal);
            continue;
        }
        stamp = stampedLock.readLock();
        try {
            result.put(entry.getKey(), value.metaSet.size());
        } finally {
            stampedLock.unlockRead(stamp);
        }
    }
    return result;
}
 
源代码12 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * readLockInterruptibly succeeds if lock free
 */
public void testReadLockInterruptibly() throws InterruptedException {
    final StampedLock lock = new StampedLock();

    long s = assertValid(lock, lock.readLockInterruptibly());
    assertTrue(lock.isReadLocked());
    lock.unlockRead(s);

    lock.asReadLock().lockInterruptibly();
    assertTrue(lock.isReadLocked());
    lock.asReadLock().unlock();
}
 
源代码13 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
}
 
源代码14 项目: jdk8u-dev-jdk   文件: Basic.java
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()); } }};
}
 
源代码15 项目: dragonwell8_jdk   文件: Basic.java
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()); } }};
}
 
源代码16 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
}
 
源代码17 项目: openjdk-8   文件: Basic.java
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()); } }};
}
 
源代码18 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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));
}
 
源代码19 项目: hottub   文件: Basic.java
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()); } }};
}
 
源代码20 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * tryConvertToReadLock succeeds for valid stamps
 */
public void testTryConvertToReadLock() throws InterruptedException {
    StampedLock lock = new StampedLock();
    long s, p;

    assertEquals(0L, lock.tryConvertToReadLock(0L));

    s = assertValid(lock, lock.tryOptimisticRead());
    p = assertValid(lock, lock.tryConvertToReadLock(s));
    assertTrue(lock.isReadLocked());
    assertEquals(1, lock.getReadLockCount());
    assertTrue(lock.validate(s));
    lock.unlockRead(p);

    s = assertValid(lock, lock.tryOptimisticRead());
    lock.readLock();
    p = assertValid(lock, lock.tryConvertToReadLock(s));
    assertTrue(lock.isReadLocked());
    assertEquals(2, lock.getReadLockCount());
    lock.unlockRead(p);
    lock.unlockRead(p);
    assertUnlocked(lock);

    for (BiConsumer<StampedLock, Long> readUnlocker : readUnlockers()) {
        for (Function<StampedLock, Long> writeLocker : writeLockers()) {
            s = assertValid(lock, writeLocker.apply(lock));
            p = assertValid(lock, lock.tryConvertToReadLock(s));
            assertFalse(lock.validate(s));
            assertTrue(lock.isReadLocked());
            assertEquals(1, lock.getReadLockCount());
            readUnlocker.accept(lock, p);
        }

        for (Function<StampedLock, Long> readLocker : readLockers()) {
            s = assertValid(lock, readLocker.apply(lock));
            assertEquals(s, lock.tryConvertToReadLock(s));
            assertTrue(lock.validate(s));
            assertTrue(lock.isReadLocked());
            assertEquals(1, lock.getReadLockCount());
            readUnlocker.accept(lock, s);
        }
    }
}