I have this (simplified) scenario:
@Test
public void testSameThread() throws InterruptedException {
ReentrantReadWriteLock rwlock = new ReentrantReadWriteLock();
rwlock.readLock().lock();
{
if (rwlock.writeLock().tryLock(50, TimeUnit.MILLISECONDS)) {
// long-running op
Thread.sleep(100);
rwlock.writeLock().unlock();
} else {
fail();
}
}
rwlock.readLock().unlock();
}
First, I acquire a read lock, then on the SAME thread I try to acquire the write lock. My assumption is that as I am on the same thread, the write lock should be acquired.
First, what is wrong with my assumption? Then, what pattern (standard Java locking mechanism) can be used to allow arbitrary nested read/write locks for the same thread?