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