• Post Reply Bookmark Topic Watch Topic
  • New Topic

ReentrantReadWriteLock - readLock()  RSS feed

 
Arul Jose
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In ReentrantReadWriteLock class, the writeLock() method CAN return the write lock only once. Tthats obvious.

What are the factors that limit the readLock() from returning locks (I think virtually infinite no of read locks can be given) ?

The method getQueuedReaderThreads() returns the set of threads waiting to read. why should they wait to READ? Its fair to wait to WRITE.
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arul Jose:

The method getQueuedReaderThreads() returns the set of threads waiting to read. why should they wait to READ? Its fair to wait to WRITE.


The obvious case is... the reader must wait to get the lock, if a writer owns the lock. Has nothing to do with fairness, as the reader wants the data in a state that is valid.

The less obvious case is... the reader must wait to get the lock, if there is a writer waiting to get the lock earlier in the queue. This has everything to do with fairness. If the readers can get the lock any time another reader owns the lock, they can play tag with each other, effectively starving out the writers.

The algorithm is a bit more complex than that, but that is basically the jist of it.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!