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 My Locking...Advice needed... 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 "My Locking...Advice needed..." Watch "My Locking...Advice needed..." New topic

My Locking...Advice needed...

James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
Hi All,

I have implemented a locking method that in theory should work well, but it causes deadlock. The problem is that I know what is wrong but I am not quite sure how to fix it.

My locking code is:

public synchronized long lock(int recNo) throws RecordNotFoundException {

// Check if the database contains the specified record.
// If not throw a RecordNotFound exception.

// Check if the required record is already locked, If not locked, create
// lock for it.
// If locked, do this:
Object recordLock = lockedRecords.get(new Integer(recNo));

// Wait until the record is unlocked.
while (this.isLocked(recNo)) {
// Wait on the record lock itself, therefore the thread
// will only wake when the required lock is released, as
// oppose to waiting on the lock manager which would result
// in threads waking when any lock is released even if the
// lock they require is not released. This waistes CPU cycles.
synchronized (recordLock) {
try {
} catch (InterruptedException ie) {
log.warning("Caught: " + ie);
// Recheck that the required record still exists in the database as
// while any waiting, a previous thread may have deleted the record.
// If not in the database throw exception.

// Record is not locked, locking it.
lockedRecords.put(new Integer(recNo), new Long(cookie));
// Return lock cookie.
return cookie;

Basically, what I am doing is locking on the record lock itself, therefore when a record is unlocked it will call notifyAll on the lock, which will wake up all threads waiting only for that record. And not all threads waiting for other records, which would be the case if I called wait on the lock manager object.

The problem is that to wait on the lock itself, I need to synchronize on it. But the whole lock method istelf needs to be synchronized also as two threads cannot be locking at the same instant. When I wait on the lock, the locks 'lock' is released, but not that of the lock manager, which causes deadlock.

I do need to hold both locks, but I need a way release the lock manager's lock before I call wait on the lock itself, but am not sure who to do it.

Any help or comments are welcome...



James<br />SCJP 1.4 - 92%<br />SCJD - 93%<br />SCWCD 1.4 - 95%<br />SCBCD 1.3 - 100%<br />SCEA - 92%
James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
What I am trying to do is:

get a lock on the Lock Manager so I can perform locking in isolation. Then when I establish that a lock already exists for the record I need, I synchronize on that lock and then wait on it. Therefore that thread only need to be woken when that specific record is unlocked and not when any record is unlocked.

Problem is that when i synchronize on two objects (lock manger and required lock) then wait on the lock, the lock manager is still locked to other threads, which causes deadlock.

(The scenario is detailed on the above post).

I am not sure the best way of solving this...

Any comments and help are greatly appreciated...


[ October 15, 2004: Message edited by: James Turner ]
Jared Chapman
Ranch Hand

Joined: Feb 23, 2004
Posts: 81
Hi James,

It's getting late, I can't think clearly, and I don't have a suggestion for your implementation at this time (sorry), so instead i'm going to share what I did.

In my Data class, I have the following variable:

The (key, value) pairs in this hashmap are (recNo, lockCookie).

And this is what my lock method does:

That's how I have it implemented (for now). If you have any comments or want to discuss you're implementation more, let me know. I'll be more productive tomorrow during the day.

B.S. University of Wisconsin<br />SCJP 1.4 (85%)<br />SCJD 1.4 (92%) B&S Contractors
I agree. Here's the link:
subject: My Locking...Advice needed...
It's not a secret anymore!