Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Lock Question

 
joe black
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public long lock(int recNo) throws RecordNotFoundException;
public void unlock(int recNo, long cookie) throws RecordNotFoundException, SecurityException;
Can someone give me a general idea about how to implement these functions. I've looked at some examples posted here but I am still a little confused. Also, are there any books that could help me with the project? I see several people have mentioned "Max's Book." Which book is this?
[ December 03, 2003: Message edited by: Joe Black ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
Welcome to JavaRanch.
The JavaRanch SCJD FAQ has a list of recommended books, including Max's book The Sun Certified Java Developer Exam with J2SE 1.4. Max's book (Mehran's book) is definately the best I have seen for this assignment.
Basically the idea for the lock method is:
  • create a collection to hold references to the locked records
  • when a client tries to lock a record, check in the collection to see if it is already locked
  • if it is already locked, wait for it to become unlocked
  • if it is not already locked, then put the record number and a unique reference for the client into the collection.


  • In your case, the unique reference is the long cookie. It can be any long number you want. Most people use a random number generator to create their cookies so that they have reasonably unique numbers with reasonable security.
    On the unlock side, you want to:
  • check if the record is already locked with the reference number provided
  • if so, remove it from the collection, and notify all waiting clients


  • That's the basic concept.
    You have an additional two items to take care of
  • Throwing RecordNotFoundException if the record is not found. Check your instructions - there will be a sentence stating what condition should result in this exception being thrown.
  • Throwing SecurityException. Again, check your instructions, it should tell you when you need to throw this. Probably it is just the 'else' part of the 'if' statement in the explanation of the unlock method above.


  • The other thing to watch for is that your lock and unlock methods must be thread safe. You may have multiple clients calling these methods simultaneously, and you need to make sure that they do behave correctly when two threads try to lock the same record at the same time.
    Does this help with your understanding?
    Regards, Andrew
     
    joe black
    Ranch Hand
    Posts: 103
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand now. Thanks for your help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic