1

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.

iceraven
  • 225
  • 1
  • 5
  • 16
  • 1
    You probably mean [`ReentrantReadWriteLock`](http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html). Also see [Java ReentrantReadWriteLocks - how to safely acquire write lock?](http://stackoverflow.com/questions/464784/java-reentrantreadwritelocks-how-to-safely-acquire-write-lock) SO post. – PM 77-1 Nov 22 '13 at 02:06

1 Answers1

1

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.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
by_unicorn
  • 36
  • 3