my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock(), unlock() and isLocked()" Watch "lock(), unlock() and isLocked()" New topic
Author

lock(), unlock() and isLocked()

Lucas cotta
Greenhorn

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...

Thanks!!

_____________________________________________________________________

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?

Thanks!!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

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,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
jQuery in Action, 2nd edition
 
subject: lock(), unlock() and isLocked()
 
Similar Threads
Bodgitt and Scarper : locking and unlocking
B&S: locking etc
Two designs of locking mechanism
a client could unlock a record that it has not locked?
create, delete and locking