8

What is Upgrade/Downgrade of ReentrantReadWriteLock? I see javadoc about Upgrade/Downgrade:

"Lock downgrading : Reentrancy also allows downgrading from the write lock to a read lock, by acquiring the write lock, then the read lock and then releasing the write lock. However, upgrading from a read lock to the write lock is not possible."

And a sample provided:

class CachedData {
   Object data;
   volatile boolean cacheValid;
   ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();

   void processCachedData() {
     rwl.readLock().lock();
     if (!cacheValid) {
        // upgrade lock manually
        rwl.readLock().unlock();   // must unlock first to obtain writelock
        rwl.writeLock().lock();
        if (!cacheValid) { // recheck
          data = ...
          cacheValid = true;
        }
        // downgrade lock
        rwl.readLock().lock();  // reacquire read without giving up write lock
        rwl.writeLock().unlock(); // unlock write, still hold read
     }

     use(data);
     rwl.readLock().unlock();
   }
 }

I know it talks about the relation between readLock and writeLock, but I couldn't get the clear concept from the doc. Could you give me a little more explanation? Thanks!

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130

1 Answers1

13

I believe that in this context the idea of "upgrading" and "downgrading" is based on the idea that the reader lock is, in a sense, a "weaker" lock than the writer lock. When the write lock is acquired, no other threads can acquire the lock in any form, whereas with a reader lock any other thread can acquire the read lock if it wants to.

Here, "downgrading" the lock means that if you hold the write lock, you can switch down to holding just the read lock by acquiring the read lock, then releasing the write lock. This means that you can have a thread that starts off doing something critically important (something that would prevent other threads from reading), does its work, and then switches to the lower-priority lock (the read lock) without ever being without the lock. This allows you to hold the lock continuously without getting preempted.

However, the other way doesn't work - once you're holding the read lock, you can't "upgrade" to holding the more important write lock by trying to acquire the write lock. If you tried to do this, the thread would just block until it was interrupted.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Do you mean that write lock can acquire read lock, but read lock cannot acquire write lock? – 卢声远 Shengyuan Lu Feb 22 '11 at 04:58
  • Exactly. The write lock owner can acquire the read lock without blocking, but if a read lock tries to get the write lock it will block forever. – templatetypedef Feb 22 '11 at 04:59
  • BTW. I've found a code (http://dmlloyd.blogspot.com/2010/02/safely-downgrading-write-lock-with.html) that is better than `ReentrantReadWriteLock`'s javadoc, as it *includes* error handling. – Piotr Findeisen Jul 17 '13 at 12:08
  • Related question [here](http://stackoverflow.com/questions/464784/java-reentrantreadwritelocks-how-to-safely-acquire-write-lock). I also posted there a solution to upgrade a read lock to a write lock, by means of a different type of lock altogether, a read-write-update lock. – npgall Sep 13 '13 at 12:05
  • New link for the downgrade pattern: http://word-bits.flurg.com/safely-downgrading-a-write-lock-with-readwritelock/ – yawkat Jan 02 '18 at 11:29
  • So I need to release read lock before trying to get write lock. Thx. – WesternGun Oct 28 '22 at 10:28