use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.
the class OPartitionedLockManager method acquireExclusiveLock.
public Lock acquireExclusiveLock(long value) {
final int hashCode = longHashCode(value);
final int index = index(hashCode);
if (useSpinLock) {
OReadersWriterSpinLock spinLock = spinLocks[index];
spinLock.acquireWriteLock();
return new SpinLockWrapper(false, spinLock);
}
final ReadWriteLock rwLock = locks[index];
final Lock lock = rwLock.writeLock();
lock.lock();
return lock;
}
use of java.util.concurrent.locks.ReadWriteLock in project orientdb by orientechnologies.
the class OPartitionedLockManager method releaseSharedLock.
public void releaseSharedLock(final int value) {
final int index = index(value);
if (useSpinLock) {
OReadersWriterSpinLock spinLock = spinLocks[index];
spinLock.releaseReadLock();
return;
}
final ReadWriteLock rwLock = locks[index];
rwLock.readLock().unlock();
}
use of java.util.concurrent.locks.ReadWriteLock in project uPortal by Jasig.
the class ReadWriteLockTemplate method doWithLock.
public static <T> T doWithLock(ReadWriteLock readWriteLock, ReadWriteCallback<T> c) {
final ReadResult<T> readResult;
final Lock readLock = readWriteLock.readLock();
readLock.lock();
try {
readResult = c.doInReadLock();
} finally {
readLock.unlock();
}
if (!readResult.isDoWriteLock()) {
return readResult.getResult();
}
final Lock writeLock = readWriteLock.writeLock();
writeLock.lock();
try {
return c.doInWriteLock(readResult);
} finally {
writeLock.unlock();
}
}
use of java.util.concurrent.locks.ReadWriteLock in project java8-tutorial by winterbe.
the class Lock3 method main.
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Map<String, String> map = new HashMap<>();
ReadWriteLock lock = new ReentrantReadWriteLock();
executor.submit(() -> {
lock.writeLock().lock();
try {
ConcurrentUtils.sleep(1);
map.put("foo", "bar");
} finally {
lock.writeLock().unlock();
}
});
Runnable readTask = () -> {
lock.readLock().lock();
try {
System.out.println(map.get("foo"));
ConcurrentUtils.sleep(1);
} finally {
lock.readLock().unlock();
}
};
executor.submit(readTask);
executor.submit(readTask);
ConcurrentUtils.stop(executor);
}
use of java.util.concurrent.locks.ReadWriteLock in project deltaspike by apache.
the class LockSupplierStorage method getLockSupplier.
protected LockSupplier getLockSupplier(final InvocationContext ic) {
final Method key = ic.getMethod();
LockSupplier operation = lockSuppliers.get(key);
if (operation == null) {
final Class declaringClass = key.getDeclaringClass();
final AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(declaringClass);
final AnnotatedMethod<?> annotatedMethod = AnnotatedMethods.findMethod(annotatedType, key);
Locked config = annotatedMethod.getAnnotation(Locked.class);
if (config == null) {
config = annotatedType.getAnnotation(Locked.class);
}
final Locked.LockFactory factory = config.factory() != Locked.LockFactory.class ? Locked.LockFactory.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(config.factory())), Locked.LockFactory.class, null)) : this;
final ReadWriteLock writeLock = factory.newLock(annotatedMethod, config.fair());
final long timeout = config.timeoutUnit().toMillis(config.timeout());
final Lock lock = config.operation() == READ ? writeLock.readLock() : writeLock.writeLock();
if (timeout > 0) {
operation = new LockSupplier() {
@Override
public Lock get() {
try {
if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
throw new IllegalStateException("Can't lock for " + key + " in " + timeout + "ms");
}
} catch (final InterruptedException e) {
Thread.interrupted();
throw new IllegalStateException("Locking interrupted", e);
}
return lock;
}
};
} else {
operation = new LockSupplier() {
@Override
public Lock get() {
lock.lock();
return lock;
}
};
}
final LockSupplier existing = lockSuppliers.putIfAbsent(key, operation);
if (existing != null) {
operation = existing;
}
}
return operation;
}
Aggregations