Search in sources :

Example 41 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project ignite by apache.

the class ReadWriteLockMultiThreadedTest method testWriteLockAcquire.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testWriteLockAcquire() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    X.println("Read lock acquired.");
    IgniteInternalFuture fut1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire write lock: " + lock);
            lock.writeLock().lock();
            try {
                X.println("Write lock acquired: " + lock);
                return null;
            } finally {
                lock.writeLock().unlock();
            }
        }
    }, 1, "write-lock");
    Thread.sleep(2000);
    IgniteInternalFuture fut2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            X.println("Attempting to acquire read lock: " + lock);
            lock.readLock().lock();
            try {
                X.println("Read lock acquired: " + lock);
                return null;
            } finally {
                lock.readLock().unlock();
            }
        }
    }, 1, "read-lock");
    Thread.sleep(2000);
    X.println(">>> Dump threads now! <<<");
    Thread.sleep(15 * 1000);
    X.println("Read lock released: " + lock);
    lock.readLock().unlock();
    fut1.get();
    fut2.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project tomee by apache.

the class SingletonInstanceManager method createInstance.

private Instance createInstance(final ThreadContext callContext, final BeanContext beanContext) throws ApplicationException {
    try {
        initializeDependencies(beanContext);
        final InstanceContext context = beanContext.newInstance();
        if (context.getBean() instanceof SessionBean) {
            final Operation originalOperation = callContext.getCurrentOperation();
            try {
                callContext.setCurrentOperation(Operation.CREATE);
                final Method create = beanContext.getCreateMethod();
                final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList<InterceptorData>(), new HashMap());
                ejbCreate.invoke();
            } finally {
                callContext.setCurrentOperation(originalOperation);
            }
        }
        final ReadWriteLock lock;
        if (beanContext.isBeanManagedConcurrency()) {
            // Bean-Managed Concurrency
            lock = new BeanManagedLock();
        } else {
            // Container-Managed Concurrency
            lock = new ReentrantReadWriteLock();
        }
        return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext(), lock);
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        final String t = "The bean instance " + beanContext.getDeploymentID() + " threw a system exception:" + e;
        logger.error(t, e);
        throw new ApplicationException(new NoSuchEJBException("Singleton failed to initialize").initCause(e));
    }
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) HashMap(java.util.HashMap) SystemInstance(org.apache.openejb.loader.SystemInstance) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method) SessionBean(javax.ejb.SessionBean) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApplicationException(org.apache.openejb.ApplicationException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) InstanceContext(org.apache.openejb.core.InstanceContext) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData)

Example 43 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.

the class DistributedLockTest method simpleLock.

@Test
public void simpleLock() {
    ReadWriteLock accountLock = manager.getLock("account");
    ReadWriteLock transactionLock = manager.getLock("transaction");
    try {
        accountLock.readLock().lock();
        transactionLock.writeLock().lock();
        Thread.sleep(1000);
    } catch (final InterruptedException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        fail();
    } finally {
        accountLock.readLock().unlock();
        transactionLock.writeLock().unlock();
    }
    assertTrue(true);
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Test(org.junit.Test)

Example 44 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.

the class DistributedLockTest method reentrantReadTest.

@Test
public void reentrantReadTest() throws InterruptedException {
    int count = 0;
    ReadWriteLock lock = manager.getLock("reentrant");
    try {
        lock.readLock().lock();
        count++;
        lock.readLock().lock();
        count++;
        lock.readLock().lock();
        count++;
        lock.readLock().lock();
        count++;
    } finally {
        lock.readLock().unlock();
        lock.readLock().unlock();
        lock.readLock().unlock();
        lock.readLock().unlock();
    }
    assertEquals(4, count);
    Thread.sleep(1000);
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Test(org.junit.Test)

Example 45 with ReadWriteLock

use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.

the class DistributedLockTest method reentrantWriteTest.

@Test
public void reentrantWriteTest() throws InterruptedException {
    int count = 0;
    ReadWriteLock lock1 = manager.getLock("reentrant");
    ReadWriteLock lock2 = manager.getLock("reentrant2");
    try {
        lock1.writeLock().lock();
        lock2.writeLock().lock();
        count++;
        lock1.writeLock().lock();
        lock2.writeLock().lock();
        count++;
        lock1.writeLock().lock();
        lock2.writeLock().lock();
        count++;
        lock1.writeLock().lock();
        lock2.writeLock().lock();
        count++;
    } finally {
        lock1.writeLock().unlock();
        lock1.writeLock().unlock();
        lock1.writeLock().unlock();
        lock1.writeLock().unlock();
        lock2.writeLock().unlock();
        lock2.writeLock().unlock();
        lock2.writeLock().unlock();
        lock2.writeLock().unlock();
    }
    assertEquals(4, count);
    Thread.sleep(1000);
}
Also used : ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Test(org.junit.Test)

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 Ignore (org.junit.Ignore)2 Member (cz.metacentrum.perun.core.api.Member)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Random (java.util.Random)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Phaser (java.util.concurrent.Phaser)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1