7

The following is the typical reader and writer pattern (a lot of reads and few writes)

  private ReadWriteLock lock = new ReentrantReadWriteLock();
  private int value;

  public void writeValue(int newValue){
       lock.writeLock().lock();
       try{
           this.value = newValue;
       }
       finally{
           lock.writeLock().unlock();
       }
  }

  public int readValue(){
       lock.readLock().lock();
       try{
           return value;
       }
       finally{
           lock.writeLock().unlock();
       }
  }

I am wondering that is it possible to have priority to writer and reader ? For example, normally writer could wait a very long time (maybe forever) if there are constantly read locks held by other thread, so is it possible to have writer with higher priority, so whenever a writer comes it can be considered as it's being as high priority (skip line) something like that.

peter
  • 8,333
  • 17
  • 71
  • 94

2 Answers2

5

According to the javadoc, the jdk implementation does not have any reader/writer priority. however, if you use the "fair" implementation, then the lock is granted in fifo order (still no reader/writer preference), so at least future readers will not block waiting writers.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Do you think other additional concurrent collection is necessary at this point ? like BlockingQueue ? or Concurrent PriortyQueue something like that ? – peter Aug 12 '12 at 19:25
  • @user1389813 - is the fifo ordering not a strong enough guarantee for your needs? considering that the readers (in your example) are only reading a simple value, i can't imagine that a writer will be blocked all that long waiting for existing readers (which can proceed concurrently). – jtahlborn Aug 12 '12 at 19:34
  • @user1389813 - use the ReentrantReadWriteLock in "fair" mode, e.g. `new ReentrantReadWriteLock(true)`. – jtahlborn Aug 12 '12 at 19:39
  • Can you briefly explain what's the ReentrantReadWriteLock in 'fair' mode ? how's it different ? – peter Aug 12 '12 at 19:41
  • 1
    @user1389813 - i _did_ explain in my answer, and it is stated even more verbosely in the javadoc to which i linked. – jtahlborn Aug 12 '12 at 19:44
0

Write locks are actually already prioritized above reader locks.

If a thread wants to read the resource, it is okay as long as no threads are writing to it, and no threads have requested write access to the resource. By up-prioritizing write-access requests we assume that write requests are more important than read-requests.

please refer to this excellent post for more details.

Edit: it is important to note that this holds for the fair mode only. thanks @jtahlborn!

Vitaliy
  • 8,044
  • 7
  • 38
  • 66
  • 1
    according to the [javadoc](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html): "This class does not impose a reader or writer preference ordering for lock access". the article you linked is a separate implementation. – jtahlborn Aug 12 '12 at 19:11
  • @jtahlborn please read on: "A thread that tries to acquire a fair read lock (non-reentrantly) will block if either the write lock is held, or there is a waiting writer thread." – Vitaliy Aug 12 '12 at 19:17
  • 1
    yes, for the _fair_ version, and that is not _writer_ priority. please see my answer. – jtahlborn Aug 12 '12 at 19:18
  • 1
    to reiterate, even in "fair" mode, there is _no_ writer priority. – jtahlborn Aug 12 '12 at 19:21