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 Project: openjdk-8   Author: bpupadhyaya   File: Basic.java    License: 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 Project: jdk8u_jdk   Author: JetBrains   File: Basic.java    License: 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 #3
Source Project: hottub   Author: dsrg-uoft   File: Basic.java    License: 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 #4
Source Project: sofa-jraft   Author: sofastack   File: RouteTable.java    License: 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 #5
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: StampedLockTest.java    License: 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 #6
Source Project: jdk8u-jdk   Author: frohoff   File: Basic.java    License: 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 #7
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Basic.java    License: 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 #8
Source Project: hottub   Author: dsrg-uoft   File: Basic.java    License: 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 #9
Source Project: dragonwell8_jdk   Author: alibaba   File: Basic.java    License: 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 #10
Source Project: jdk8u-dev-jdk   Author: frohoff   File: Basic.java    License: 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 #11
Source Project: dragonwell8_jdk   Author: alibaba   File: Basic.java    License: 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 #12
Source Project: TencentKona-8   Author: Tencent   File: Basic.java    License: 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 #13
Source Project: blazingcache   Author: diennea   File: KeyedLockManager.java    License: 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 #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: StampedLockTest.java    License: 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 #15
Source Project: TencentKona-8   Author: Tencent   File: Basic.java    License: 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 #16
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Basic.java    License: 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 #17
Source Project: jdk8u-dev-jdk   Author: frohoff   File: Basic.java    License: 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 #18
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: Basic.java    License: 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 #19
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: Basic.java    License: 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 #20
Source Project: native-obfuscator   Author: radioegor146   File: Basic.java    License: 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 #21
Source Project: jdk8u-dev-jdk   Author: frohoff   File: Basic.java    License: 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 #22
Source Project: openjdk-8-source   Author: keerath   File: Basic.java    License: 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 #23
Source Project: openjdk-8-source   Author: keerath   File: Basic.java    License: 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 #24
Source Project: openjdk-8   Author: bpupadhyaya   File: Basic.java    License: 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 #25
Source Project: jdk8u60   Author: chenghanpeng   File: Basic.java    License: 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 #26
Source Project: jdk8u60   Author: chenghanpeng   File: Basic.java    License: 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 #27
Source Project: jdk8u60   Author: chenghanpeng   File: Basic.java    License: 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 #28
Source Project: nuls-v2   Author: nuls-io   File: ChainContext.java    License: 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 #29
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: Basic.java    License: 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 #30
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Basic.java    License: GNU General Public License v2.0 5 votes vote down vote up
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(); } }};
}