I have a bufferPool which stores a lot of objects. In order to prevent different threads writing the same object and at the same for efficiency I use ReadWriteLock. But I am not too sure about how it works. After I find the object I want to access,
if(perm.equals(Permissions.READ_ONLY)) {
readLock.lock();
} else if(Permissions.READ_WRITE) {
writeLock.lock();
}
return the object I want to access
How does the system know which object the program is trying to access and lock it?
Or my syntax here is wrong? Help!!! Thanks
The system doesn’t know what you’re trying to lock. If you want to use lock objects for a series of items, you’ll have to have a lock for each item, or use one lock for the entire object set (which could be a big bottleneck). This is an example where a single lock is used to synchronize access.