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();
}
}
};
}
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();
}
}
};
}
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);
}
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);
}
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);
}
Aggregations