java.util.concurrent.locks.StampedLock Java Examples

The following examples show how to use java.util.concurrent.locks.StampedLock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Basic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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(); } }};
}
 
Example #2
Source File: RouteTable.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: Basic.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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(); } }};
}
 
Example #5
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #6
Source File: Basic.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #7
Source File: Basic.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #8
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #9
Source File: Basic.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #10
Source File: ChainContext.java    From nuls-v2 with MIT License 6 votes vote down vote up
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);
}
 
Example #11
Source File: Basic.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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(); } }};
}
 
Example #12
Source File: Basic.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #13
Source File: Basic.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #14
Source File: Basic.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #15
Source File: Basic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #16
Source File: Basic.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #17
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #18
Source File: Basic.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #19
Source File: Basic.java    From native-obfuscator with GNU General Public License v3.0 6 votes vote down vote up
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(); } }};
}
 
Example #20
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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(); } }};
}
 
Example #21
Source File: Basic.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #22
Source File: Basic.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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(); } }};
}
 
Example #23
Source File: Basic.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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();}};
}
 
Example #24
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #25
Source File: Basic.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #26
Source File: Basic.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #27
Source File: Basic.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #28
Source File: KeyedLockManager.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #29
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
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()); } }};
}
 
Example #30
Source File: StampedLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}