• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about Reentrant Locks  RSS feed

 
Lakshithaa Ranasinghe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am currently studying for OCPJP and I got a question regarding ReentrantLocks.



It says both Pieces of code are equivalent. What I don't get is in First piece synchronized block it acquires the lock on obj object. But on what object does Reentrant lock acquire the lock on? Can I specify the object I want to lock on just like synchronized(obj)?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshithaa Ranasinghe wrote:
It says both Pieces of code are equivalent. What I don't get is in First piece synchronized block it acquires the lock on obj object. But on what object does Reentrant lock acquire the lock on? Can I specify the object I want to lock on just like synchronized(obj)?


The Reentrant lock class doesn't actually acquire any synchronization locks (via the synchronized keyword). The instance itself is the lock (albeit a completely different implementation) -- acquired via the lock() method call.... it is your responsibility to lock() and unlock() as needed, and access the object/resource that you are trying to protect accordingly.

Henry
 
Lakshithaa Ranasinghe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Reentrant lock class doesn't actually acquire any synchronization locks (via the synchronized keyword). The instance itself is the lock (albeit a completely different implementation) -- acquired via the lock() method call.... it is your responsibility to lock() and unlock() as needed, and access the object/resource that you are trying to protect accordingly.



could you explain it a little bit? Does that mean any recourse used between that try block (in my example) is protected?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lakshithaa Ranasinghe wrote:
could you explain it a little bit? Does that mean any recourse used between that try block (in my example) is protected?


Of course not !! ... This would not be true even if you are using the synchronized block mechanism.

The locks must be used in a cooperative manner. The resources that you are trying to protect don't magically get protected via the locks (with either type). They get protected because the threads are using them in a thread safe manner. And it is able to do that because they correctly acquire the locks as needed.

If you have a thread that doesn't use the locks. Or if the threads use the locks incorrectly (depending on what the resources need to be thread safe), then it will break thread safety.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!