I'm referring to the following link: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html
Could someone explain to me what the read lock do. Does it just allow read access for concurrent threads to read and not write? As for write lock, it prevents concurrent threads from reading and writing until it is unlocked?
I'm especially confused with the read lock. Why and when should i implement a read lock when concurrency could happen.
ReentrantLock
is an implement for Lock
interface, it provides a more flexible way than synchronized
keyword, and add the tryLock
function.
ReentrantReadWriteLock
is an implement for ReadWriteLock
interface, this class provides a solution to Reader-Writer problem.
For the write lock, it is an exclusive lock, it will prevents both reading and writing threads.
For the read lock, it allows multiply reader threads to access at the same time, and prevent the writer thread when the reader count is more than zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.