类java.util.concurrent.locks.StampedLock源码实例Demo

下面列出了怎么用java.util.concurrent.locks.StampedLock的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: openjdk-8   文件: Basic.java
static Reader interruptibleReaderView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Reader("InterruptibleReaderView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        final Lock rl = sl.asReadLock();

        try {
            if (timeout < 0)
                rl.lockInterruptibly();
            else
                rl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) rl.unlock(); } }};
}
 
源代码2 项目: jdk8u_jdk   文件: Basic.java
static Writer interruptibleWriter(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Writer("InterruptibleWriter") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.writeLockInterruptibly());
            else
                stamp(sl.tryWriteLock(timeout, unit));
            check(sl.validate(stamp()));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
 
源代码3 项目: hottub   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码4 项目: sofa-jraft   文件: RouteTable.java
/**
 * Update leader info.
 *
 * @param groupId raft group id
 * @param leader  peer of leader
 * @return true on success
 */
public boolean updateLeader(final String groupId, final PeerId leader) {
    Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");

    if (leader != null) {
        // If leader presents, it should not be empty.
        Requires.requireTrue(!leader.isEmpty(), "Empty leader");
    }

    final GroupConf gc = getOrCreateGroupConf(groupId);
    final StampedLock stampedLock = gc.stampedLock;
    final long stamp = stampedLock.writeLock();
    try {
        gc.leader = leader;
    } finally {
        stampedLock.unlockWrite(stamp);
    }
    return true;
}
 
源代码5 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * 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);
}
 
源代码6 项目: jdk8u-jdk   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码7 项目: openjdk-jdk9   文件: Basic.java
static Writer interruptibleWriter(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Writer("InterruptibleWriter") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.writeLockInterruptibly());
            else
                stamp(sl.tryWriteLock(timeout, unit));
            check(sl.validate(stamp()));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
 
源代码8 项目: 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()); } }};
}
 
源代码9 项目: dragonwell8_jdk   文件: Basic.java
static Writer interruptibleWriterView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Writer("InterruptibleWriterView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        Lock wl = sl.asWriteLock();
        try {
            if (timeout < 0)
                wl.lockInterruptibly();
            else
                wl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) wl.unlock(); } }};
}
 
源代码10 项目: jdk8u-dev-jdk   文件: Basic.java
static Iterator<Reader> readerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Reader>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Reader next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return reader(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleReader(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleReader(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码11 项目: dragonwell8_jdk   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码12 项目: TencentKona-8   文件: 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()); } }};
}
 
源代码13 项目: blazingcache   文件: KeyedLockManager.java
private StampedLock makeLockForKey(RawString key) {
    StampedLock lock;
    generalLock.lock();
    try {
        lock = liveLocks.get(key);
        if (lock == null) {
            lock = makeLock();
            liveLocks.put(key, lock);
            locksCounter.put(key, new AtomicInteger(1));
        } else {
            locksCounter.get(key).incrementAndGet();
        }
    } finally {
        generalLock.unlock();
    }
    return lock;
}
 
源代码14 项目: openjdk-jdk9   文件: StampedLockTest.java
/**
 * write-locking, then unlocking, an unlocked lock succeed
 */
public void testWriteLock_lockUnlock() {
    StampedLock lock = new StampedLock();

    for (Function<StampedLock, Long> writeLocker : writeLockers())
    for (BiConsumer<StampedLock, Long> writeUnlocker : writeUnlockers()) {
        assertFalse(lock.isWriteLocked());
        assertFalse(lock.isReadLocked());
        assertEquals(0, lock.getReadLockCount());

        long s = writeLocker.apply(lock);
        assertValid(lock, s);
        assertTrue(lock.isWriteLocked());
        assertFalse(lock.isReadLocked());
        assertEquals(0, lock.getReadLockCount());
        writeUnlocker.accept(lock, s);
        assertUnlocked(lock);
    }
}
 
源代码15 项目: TencentKona-8   文件: Basic.java
static Writer interruptibleWriterView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Writer("InterruptibleWriterView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        Lock wl = sl.asWriteLock();
        try {
            if (timeout < 0)
                wl.lockInterruptibly();
            else
                wl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) wl.unlock(); } }};
}
 
源代码16 项目: openjdk-jdk9   文件: Basic.java
static Iterator<Reader> readerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Reader>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Reader next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return reader(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleReader(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleReader(sl, LONG_DELAY_MS, MILLISECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码17 项目: jdk8u-dev-jdk   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码18 项目: openjdk-jdk8u-backup   文件: Basic.java
static Writer interruptibleWriter(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Writer("InterruptibleWriter") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.writeLockInterruptibly());
            else
                stamp(sl.tryWriteLock(timeout, unit));
            check(sl.validate(stamp()));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
 
源代码19 项目: jdk8u-jdk   文件: Basic.java
static Writer interruptibleWriter(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Writer("InterruptibleWriter") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.writeLockInterruptibly());
            else
                stamp(sl.tryWriteLock(timeout, unit));
            check(sl.validate(stamp()));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
 
源代码20 项目: native-obfuscator   文件: Basic.java
static Writer interruptibleWriterView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Writer("InterruptibleWriterView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        Lock wl = sl.asWriteLock();
        try {
            if (timeout < 0)
                wl.lockInterruptibly();
            else
                wl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) wl.unlock(); } }};
}
 
源代码21 项目: jdk8u-dev-jdk   文件: Basic.java
static Reader interruptibleReaderView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Reader("InterruptibleReaderView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        final Lock rl = sl.asReadLock();

        try {
            if (timeout < 0)
                rl.lockInterruptibly();
            else
                rl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(sl.isReadLocked());
            check(!sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) rl.unlock(); } }};
}
 
源代码22 项目: openjdk-8-source   文件: 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()); } }};
}
 
源代码23 项目: openjdk-8-source   文件: Basic.java
static Writer interruptibleWriter(final StampedLock sl,
                                  final long timeout,
                                  final TimeUnit unit,
                                  final Phaser gate) {
    return new Writer("InterruptibleWriter") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        try {
            if (timeout < 0)
                stamp(sl.writeLockInterruptibly());
            else
                stamp(sl.tryWriteLock(timeout, unit));
            check(sl.validate(stamp()));
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
 
源代码24 项目: openjdk-8   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码25 项目: jdk8u60   文件: Basic.java
static Writer interruptibleWriterView(final StampedLock sl,
                                      final long timeout,
                                      final TimeUnit unit,
                                      final Phaser gate) {
    return new Writer("InterruptibleWriterView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        Lock wl = sl.asWriteLock();
        try {
            if (timeout < 0)
                wl.lockInterruptibly();
            else
                wl.tryLock(timeout, unit);
            stamp(1L);  // got the lock
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } catch (Throwable x) { thrown(x);
        } finally { if (stamp() != 0L) wl.unlock(); } }};
}
 
源代码26 项目: jdk8u60   文件: Basic.java
static Iterator<Reader> readerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Reader>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Reader next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return reader(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleReader(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleReader(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码27 项目: jdk8u60   文件: Basic.java
static Iterator<Writer> writerIterator(final StampedLock sl,
                                       final Phaser gate) {
    return new Iterator<Writer>() {
        int i = 0;
        boolean view = false;
        public boolean hasNext() { return true; }
        public Writer next() {
            switch ((i++)&7) {
                case 1: case 4: case 7:
                    return writer(sl, gate, view ^= true);
                case 2: case 5:
                    return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
                default:
                    return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
        public void remove() {throw new UnsupportedOperationException();}};
}
 
源代码28 项目: nuls-v2   文件: ChainContext.java
public void init() {
    LoggerUtil.init(chainId);
    cachedBlockSize = new AtomicInteger(0);
    this.setStatus(StatusEnum.INITIALIZING);
    cachedHashHeightMap = CollectionUtils.getSynSizedMap(parameters.getSmallBlockCache());
    orphanBlockRelatedNodes = CollectionUtils.getSynSizedMap(parameters.getHeightRange());
    packingAddressList = CollectionUtils.getSynList();
    duplicateBlockMap = new HashMap<>();
    systemTransactionType = new ArrayList<>();
    needSyn = true;
    lock = new StampedLock();
    //各类缓存初始化
    SmallBlockCacher.init(chainId);
    SingleBlockCacher.init(chainId);
    BlockChainManager.init(chainId);
    TxGroupRequestor.init(chainId);
}
 
源代码29 项目: 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()); } }};
}
 
源代码30 项目: openjdk-jdk9   文件: Basic.java
static Writer writerView(final StampedLock sl, final Phaser gate) {
    return new Writer("WriterView") { public void run() {
        if (gate != null ) toTheStartingGate(gate);
        Lock wl = sl.asWriteLock();
        wl.lock();
        try {
            stamp(1L);  // got the lock
            check(!sl.isReadLocked());
            check(sl.isWriteLocked());
        } finally { wl.unlock(); } }};
}