Search in sources :

Example 31 with ReadWriteLock

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;
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 32 with ReadWriteLock

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();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 33 with ReadWriteLock

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();
    }
}
Also used : Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 34 with ReadWriteLock

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);
}
Also used : HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ExecutorService(java.util.concurrent.ExecutorService) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 35 with ReadWriteLock

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;
}
Also used : Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Locked(org.apache.deltaspike.core.api.lock.Locked) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)45 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)36 Lock (java.util.concurrent.locks.Lock)21 Test (org.junit.Test)8 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 ArrayList (java.util.ArrayList)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Ignore (org.junit.Ignore)2 Member (cz.metacentrum.perun.core.api.Member)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1