4

I have some data structure, in which I want to exclusively lock the access for writing, but to enable parallel access for reading.

I made some searches and found out the classes ReadWriteLock and ReentrantReadWriteLock - which both supply a read-lock and a write-lock.

I didn't understand what is the difference between them. Could someone please explain?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • 5
    To start with, one is an interface and the other is a concrete class... – assylias Jun 20 '16 at 13:25
  • https://en.wikipedia.org/wiki/Reentrant_mutex – D Levant Jun 20 '16 at 13:26
  • @assylias, thank you. I briefly looked at the header of the Javadoc and missed the `Interface` keyword written at the top. Feeling stupid for the question... Anyway, I hope this thread could save some minutes to some guys in the future – SomethingSomething Jun 20 '16 at 13:29

1 Answers1

5

I am feeling stupid for my question - the answer is very simple... I hope I'll save time to other people though..

ReadWriteLock is simply an interface, while ReentrantReadWriteLock is an implementation of it..

So you can't actually use "an instance of ReadWriteLock"...

That's the whole answer...

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
  • Well, you should still understand what Reentrant means. – D Levant Jun 20 '16 at 13:28
  • I guess it means that if it's about reading, then multiple threads can access, and if it's about modification, then only the modifying thread can access.. Right? It's about shared and exclusive locks – SomethingSomething Jun 20 '16 at 13:31
  • 1
    No, that is the ReadWrite part. The built in Java locks (synchronized keyword) are reentrant for example, without being read write locks. Look at what I linked in my other comment https://en.wikipedia.org/wiki/Reentrant_mutex – D Levant Jun 20 '16 at 13:34
  • @Holger you should know that we need to wait 2 days before accepting our own answer – Nicolas Filotto Jun 20 '16 at 18:24