Search in sources :

Example 1 with StampedLock

use of java.util.concurrent.locks.StampedLock in project jdk8u_jdk by JetBrains.

the class Basic method readerView.

static Reader readerView(final StampedLock sl, final Phaser gate) {
    return new Reader("ReaderView") {

        public void run() {
            if (gate != null)
                toTheStartingGate(gate);
            final Lock rl = sl.asReadLock();
            rl.lock();
            try {
                // got the lock
                stamp(1L);
                check(sl.isReadLocked());
                check(!sl.isWriteLocked());
            } finally {
                rl.unlock();
            }
        }
    };
}
Also used : Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) StampedLock(java.util.concurrent.locks.StampedLock)

Example 2 with StampedLock

use of java.util.concurrent.locks.StampedLock in project jdk8u_jdk by JetBrains.

the class Basic method writerView.

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 {
                // got the lock
                stamp(1L);
                check(!sl.isReadLocked());
                check(sl.isWriteLocked());
            } finally {
                wl.unlock();
            }
        }
    };
}
Also used : Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) StampedLock(java.util.concurrent.locks.StampedLock)

Example 3 with StampedLock

use of java.util.concurrent.locks.StampedLock in project herddb by diennea.

the class LocalLockManager method releaseWriteLockForKey.

public void releaseWriteLockForKey(Bytes key, LockHandle lockStamp) {
    StampedLock lock = returnLockForKey(key);
    lock.unlockWrite(lockStamp.stamp);
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 4 with StampedLock

use of java.util.concurrent.locks.StampedLock in project herddb by diennea.

the class LocalLockManager method releaseReadLockForKey.

public void releaseReadLockForKey(Bytes key, LockHandle lockStamp) {
    StampedLock lock = returnLockForKey(key);
    lock.unlockRead(lockStamp.stamp);
}
Also used : StampedLock(java.util.concurrent.locks.StampedLock)

Example 5 with StampedLock

use of java.util.concurrent.locks.StampedLock in project java8-tutorial by winterbe.

the class Lock5 method main.

public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(2);
    StampedLock lock = new StampedLock();
    executor.submit(() -> {
        long stamp = lock.tryOptimisticRead();
        try {
            System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
            ConcurrentUtils.sleep(1);
            System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
            ConcurrentUtils.sleep(2);
            System.out.println("Optimistic Lock Valid: " + lock.validate(stamp));
        } finally {
            lock.unlock(stamp);
        }
    });
    executor.submit(() -> {
        long stamp = lock.writeLock();
        try {
            System.out.println("Write Lock acquired");
            ConcurrentUtils.sleep(2);
        } finally {
            lock.unlock(stamp);
            System.out.println("Write done");
        }
    });
    ConcurrentUtils.stop(executor);
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) StampedLock(java.util.concurrent.locks.StampedLock)

Aggregations

StampedLock (java.util.concurrent.locks.StampedLock)15 Lock (java.util.concurrent.locks.Lock)5 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)5 ExecutorService (java.util.concurrent.ExecutorService)3 HttpServerExchange (io.undertow.server.HttpServerExchange)1 HashMap (java.util.HashMap)1 Phaser (java.util.concurrent.Phaser)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Consumer (java.util.function.Consumer)1 LongConsumer (java.util.function.LongConsumer)1 Session (org.wildfly.clustering.web.session.Session)1 OOBSession (org.wildfly.clustering.web.session.oob.OOBSession)1