aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Lock Manager question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Lock Manager question" Watch "Lock Manager question" New topic
Author

Lock Manager question

Sumesh Aravindan
Greenhorn

Joined: Jul 09, 2002
Posts: 14
Hi,
I wanted to make sure that I have implemented the locks properly.
I have a lock manager class that is a singleton. Lock manager has 2 public methods( lock and unlock). The lock manager also has a hashmap to keep track of all the records that are currently locked. The records are stored in the hashmap with the record-number as its key.
Please give me more suggestions on lock manager..
Thanks,
Sumesh
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi Sumesh,
That sounds ok to me, though I think a formal lockmanager is somewhat overkill. I suggest that you write a multithreaded test client, and see how it all holds up.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
Sumesh Aravindan
Greenhorn

Joined: Jul 09, 2002
Posts: 14
Hi Max,
Thanks for your reply.
though I think a formal lockmanager is somewhat overkill

could you please elaborate ..coz' I am not very clear on this.
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Max,
How can the record No be a key. I think the key should be the clientID are the remoteObject. Correct me if iam wrong.
-rameshkumar
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
Hi,
The record number is indeed the key IMO. The client ID cannot, as you could have a same client own several locks at the same time. Then you would want to have several entries with the same key. (Or then you have to use a list in the value attribute of your map, which makes things more difficult).
On the other way, if you just have your record number be the key, all you need is to put a reference to your client (client ID, ...) as value of the map.
Hope this helps
Stephane
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi Stephane,
I agree what u say. If i have clientID as key and lets assume two request comes to the server at the same time to book different records then being ClientID is the key unnecesserly it has to wait, Where its not needed in case of having recordNo as key.
Is it wright to do locking using HashSet which uses recNo alone.
thanks,
rameshkumar
Stephane Weber
Ranch Hand

Joined: Mar 07, 2002
Posts: 110
1. Don't forget that HashSet is not synchronized : (extract from the Java API for class HashSet)

Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the HashSet instance:
Set s = Collections.synchronizedSet(new HashSet(...));

So you better synchronize your LockManager if you want to be sure that the HashSet will remain consistent with concurrent accesses.
2. It is ok to have only record number, if you find a way to be sure you grant unlocks only to those who locked ... Personnally I used a clientId to keep track of the owner of a lock. But some threads in JavaRanch discuss the other possibility.

Hope this helps
Stephane
[ October 17, 2002: Message edited by: St�phane Weber ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

"sumesh_aravind"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks! and welcome to the JavaRanch!
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lock Manager question