File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Read Write locks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Read Write locks" Watch "Read Write locks" New topic
Author

Read Write locks

Ganesh Thanga
Greenhorn

Joined: May 14, 2008
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read Write locks