This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Unlocking while downgrading lock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Unlocking while downgrading lock" Watch "Unlocking while downgrading lock" New topic

Unlocking while downgrading lock

Luc Feys

Joined: Nov 21, 2005
Posts: 20

I have a general question regarding the correct usage of a ReadWriteLock in Java 5.

Andrew's book states that if you use a lock, it's best to unlock in a finally clause, like here:

But what is the best way to unlock if you downgrade from a writeLock to a readLock?

Now I have

The readLocked and writeLocked boolean's are required to guarantee unlocking in case of an error but to prevent IllegalMonitorState exceptions to occur if no errors occur.
But I don't really feel comfortable with this solution. Does anyone have a suggestion on how to improve this?

Thanks a lot.

Luc Feys
Luc Feys

Joined: Nov 21, 2005
Posts: 20
Solved. With a little bit of refactoring I was able to restructure my code. Now I have the following

Quite simple actually. Don't know why I didn't see this last night.

Luc Feys
I agree. Here's the link:
subject: Unlocking while downgrading lock
Similar Threads
ReentrantReadWriteLock understanding
recordNumbersLock - how is it associated to recordNumbers map?
Thread Locking Confirmation
B&S: Reusing deleted records
data server