aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock(), unlock() and isLocked() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock(), unlock() and isLocked()" Watch "lock(), unlock() and isLocked()" New topic

lock(), unlock() and isLocked()

Lucas cotta

Joined: Oct 10, 2011
Posts: 21
Hi all!

In my assignment, the interface has the methods: lock(), unlock() and isLocked(). So I assume they must be used by the client. Is that correct?
If so, I imagine that the client will first call the isLocked() method as a while condition. When the desired record becomes unlocked (the while finish), the client will call the lock() method.
But between the finish of the while, and the call to the the lock() method, another client can get the lock for the record.

How can I ensure that nobody is going to get the lock for the desired record between this two operations? I can't just synchronize in the client...



EDIT: Now, reading more carefully, I noticed the instructions say that the lock method does all this wait-while-is-locked-and-then-lock work.
So, I'll change my question... For what did you guys used the isLocked() method?

Roel De Nijs

Joined: Jul 19, 2004
Posts: 6290

Hi Lucas,

Your more careful reading of the instructions is spot-on: lock-method is indeed responsible for "waiting while record is locked and lock when record is available". The isLocked is not used in my application (just like I didn't use delete and create methods), but I provided an actual (working) implementation for these unused methods.

Hope it helps!
Kind regards,

SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
I agree. Here's the link:
subject: lock(), unlock() and isLocked()