use of java.util.concurrent.locks.ReadWriteLock in project ignite by apache.
the class ReadWriteLockMultiThreadedTest method testReadLockAcquire.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testReadLockAcquire() throws Exception {
final ReadWriteLock lock = new ReentrantReadWriteLock();
lock.writeLock().lock();
X.println("Write lock acquired: " + lock);
IgniteInternalFuture fut = 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("Write lock released.");
lock.writeLock().unlock();
fut.get();
}
use of java.util.concurrent.locks.ReadWriteLock in project ignite by apache.
the class ReadWriteLockMultiThreadedTest method testTryWriteLock.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
final ReadWriteLock lock = new ReentrantReadWriteLock();
lock.readLock().lock();
X.println("Read lock acquired.");
IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Nullable
@Override
public Object call() throws Exception {
boolean res = lock.writeLock().tryLock();
X.println("Attempting to try write lock: " + res);
try {
return null;
} finally {
if (res)
lock.writeLock().unlock();
}
}
}, 1, "write-lock");
Thread.sleep(2000);
X.println("Read lock released: " + lock);
lock.readLock().unlock();
fut.get();
}
use of java.util.concurrent.locks.ReadWriteLock in project jackrabbit-oak by apache.
the class TarRevisions method setHead.
/**
* This implementation blocks if a concurrent call to
* {@link #setHead(Function, Option...)} is already in
* progress.
*
* @param options zero or one expedite option for expediting this call
* @throws IllegalArgumentException on any non recognised {@code option}.
* @see #EXPEDITE_OPTION
*/
@Override
public boolean setHead(@Nonnull RecordId expected, @Nonnull RecordId head, @Nonnull Option... options) {
checkBound();
// If the expedite option was specified we acquire the write lock instead of the read lock.
// This will cause this thread to get the lock before all threads currently waiting to
// enter the read lock. See also the class comment of ReadWriteLock.
Lock lock = isExpedited(options) ? rwLock.writeLock() : rwLock.readLock();
lock.lock();
try {
RecordId id = this.head.get();
return id.equals(expected) && this.head.compareAndSet(id, head);
} finally {
lock.unlock();
}
}
use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.
the class DistributedLockTest method writeLockTest.
@Test
public void writeLockTest() throws InterruptedException {
class ReadLockThread implements Runnable {
private final ReadWriteLock readWriteLock;
private ReadLockThread(final ReadWriteLock readWriteLock) {
this.readWriteLock = readWriteLock;
}
@Override
public void run() {
try {
logger.info("try read lock");
readWriteLock.readLock().lock();
logger.info("got read lock");
} finally {
logger.info("unlock read lock");
readWriteLock.readLock().unlock();
}
}
}
class WriteLockThread implements Runnable {
private final ReadWriteLock readWriteLock;
private WriteLockThread(final ReadWriteLock readWriteLock) {
this.readWriteLock = readWriteLock;
}
@Override
public void run() {
try {
logger.info("try write lock");
readWriteLock.writeLock().lock();
logger.info("got write lock");
Thread.sleep(3000);
} catch (InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
} finally {
logger.info("unlock write lock");
readWriteLock.writeLock().unlock();
}
}
}
ReadWriteLock rwLock = manager.getLock("test-lock");
Thread writeLockThread = new Thread(new WriteLockThread(rwLock));
Thread readLockThread = new Thread(new ReadLockThread(rwLock));
writeLockThread.start();
Thread.sleep(500);
readLockThread.start();
writeLockThread.join();
readLockThread.join();
}
use of java.util.concurrent.locks.ReadWriteLock in project jgnash by ccavanaugh.
the class DistributedLockTest method multipleReadLocks.
@Test
public void multipleReadLocks() {
class WriteLockTest extends Thread {
@Override
public void run() {
ReadWriteLock lock = manager.getLock("lock");
logger.log(Level.INFO, "locking: {0}", getId());
lock.readLock().lock();
try {
Random random = new Random();
int num = random.nextInt(3000);
try {
Thread.sleep(num);
} catch (final InterruptedException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
fail();
}
} finally {
logger.log(Level.INFO, "unlocking: {0}", getId());
lock.readLock().unlock();
}
}
}
Thread thread1 = new WriteLockTest();
Thread thread2 = new WriteLockTest();
Thread thread3 = new WriteLockTest();
Thread thread4 = new WriteLockTest();
thread1.start();
thread2.start();
thread3.start();
thread4.start();
try {
thread1.join();
thread2.join();
thread3.join();
thread4.join();
} catch (final InterruptedException e) {
Logger.getLogger(DistributedLockTest.class.getName()).log(Level.SEVERE, e.getLocalizedMessage(), e);
fail();
}
assertTrue(true);
}
Aggregations