File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Rentrant lock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Rentrant lock question" Watch "Rentrant lock question" New topic

Rentrant lock question

Ciaran Cahill

Joined: Aug 03, 2007
Posts: 10
I know there are a lot of locking questions already but anyway:

In my database class, I have several resources, such as the list of records, the list of reserved records and the random access file used to access the database binary file.
At the moment I am controlling access to all of these using a single Reentrant lock. If I need access to any of these 3 resources, I use this one lock. I also have a single Condition that is used together with this lock. This simplifies the locking and makes it easier to track down deadlocking.

I just have one doubt:
Would it be better to have a Reentrant lock for each of the 3 resources, and lock on all 3? This would make it clear which resources are being locked. - My only doubt is that 3 nested locks might not be necessary and might be bad style..

Mike Ottinger
Ranch Hand

Joined: Jan 11, 2002
Posts: 125
Hi Ciaran,

IMO it's advisable to use separate locks for each resource. No sense in having a thread block on a lock if it's being locked for a resource it's not even interested in. This is not bad form. Hope this helps.

SCJP 1.4 SCJD 1.5
I agree. Here's the link:
subject: Rentrant lock question
It's not a secret anymore!