Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read Write locks

 
Ganesh Thanga
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ? How it allows concurrent read?

if I say

read()
{
readlock.lock();
collection.get();
readlock.unlock();
}


write(a){
writeLock.lock();{
collection.put(a);
writelock.unlock();
}



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Thanga wrote:Hi,
How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ? How it allows concurrent read?

if I say

read()
{
readlock.lock();
collection.get();
readlock.unlock();
}


write(a){
writeLock.lock();{
collection.put(a);
writelock.unlock();
}



Iin the case you show here, the collection itself knows nothing about its concurrent use, and knows nothing about which threads are reading or writing it, since you're managing that externally.

It's not clear to me what you're having trouble with.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How a read write lock enabled resource (collection) know which are all are read threads and which all are write threads ?

The collection doesn't know or care if a thread is all read or all write operations. All it knows is which of its own methods are read methods and which of its own methods are write methods. It then protects its own methods with an appropriate lock. So a thread that does all reads will only get read locks, a thread which does all writes gets only write locks, and a thread which reads sometimes and which writes other times will get a read lock when it reads and a write lock when it writes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic