File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes confused with singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "confused with singleton" Watch "confused with singleton" New topic
Author

confused with singleton

Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
hi,

public class LockManager{
private static HashMap hashmap;
private static LockManager instance = null;
public static synchronized getInstance(){
if(instance==null) instance=new LockManager();
return instance;
}
public synchronized lock(int rec,Object o){...}
...
}

In my RemoteAccess I invoke the LockManager loc=LockManager.getInstance(),so All the clients will have only single LockManager ,right?
Should I put the hashmap in a static state?
Should I use 'public void synchronized lock(int,Object){}' or 'synchronized(hashmap){}'?
So if one thread comes along and place the value �1� in the HashMap all other threads must wait until that thread is finished even if they want to lock the record �5�,for the lock is synchronized.
How should I do that?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ray,

... so All the clients will have only single LockManager ,right?

Right.

Should I put the hashmap in a static state?

No need to since the LockManager is a singleton.

Should I use 'public void synchronized lock(int,Object){}' or 'synchronized(hashmap){}'?

You can do it either way, but my preference is to lock the method instead of the map. It's always riskier to synchronize an object instead of a method, although in this particular case, the risk is minimal unless the Map is somehow publically available.

So if one thread comes along and place the value �1� in the HashMap all other threads must wait until that thread is finished even if they want to lock the record �5�,for the lock is synchronized.
How should I do that?

No. Check the map with locks.containsValue(record) and if the record is not in the map, map that client to the record with locks.put(client, record); otherwise call wait(). Do the inverse in unlock.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi Michael,
No. Check the map with locks.containsValue(record) and if the record is not in the map, map that client to the record with locks.put(client, record); otherwise call wait(). Do the inverse in unlock.

I mean When Client A is booking and has lock(5),
then client B wants to book at the same time and
B's record number is 6.
For the sake of the same object to LockManager and the synchronized method 'lock()',B will not allow to process the method lock until A has been out of the block synchronized.
I know I have wrong in some place,but I don't know where it is.
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi
Can anybody help me with the above question?
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
Here is how I did mine (I lost 2 points in my server though)...
- synched on the hash map and not the methods
- when locking a row
check to see if the id, lock pair existed
if yes, synch on hash and wait
if no, put the pair in the hash (in synch block)
- when unlocking a row
check to see if id and lock existed
if yes, check to see if same id that did the lock, perform unlock if ok, synch and notify all
if no, ignore the unlock call
Now, there are a few holes between synched calls in my code and that is probably why I missed two point, but Sun doesnt actually tell you for sure
[ August 26, 2002: Message edited by: Nate Johnson ]

scwcd, scjd, scjp<br /><a href="http://natejohnson.us" target="_blank" rel="nofollow">http://natejohnson.us</a><br /><a href="http://rice.kuali.org" target="_blank" rel="nofollow">http://rice.kuali.org</a>
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
hi Nate,
congratulations!!!
Another question.How to implement the lock(-1) and when to invoke it?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ray,

I mean When Client A is booking and has lock(5),
then client B wants to book at the same time and
B's record number is 6.
For the sake of the same object to LockManager and the synchronized method 'lock()',B will not allow to process the method lock until A has been out of the block synchronized.
I know I have wrong in some place,but I don't know where it is.

That's true, but what are we talking about 1, 2 microseconds? You are not going to gain much by synching on the map instead of the method. No matter how you slice it, you have a damn turnstyle for every client to pass thru, so placing 8-10 lines of code outside the turnstyle is hardly worth the effort.
Michael Morris
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
I would have to agree with Michael... doing it the way I did it could be dangerous for a heavily used app...
About the lock(-1)... I just set a flag so that no other lock could be created. Then got a list of all of the locks from the hashmap and waited for each of them to go away...
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
hi,
Do you mean when I call the lock(-1),I should wait
all the others unlock?
But when will i invoke it?I think when the server
down we may call it,right?
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
I actually never called it... some people probably did when the server went down if they used a GUI for the server.
There is more than one way to do it... I set a flag that did not allow locks to be made and just waited for all of the other locks to go away... others have just put locks on all of the records... it is up to you really.
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
thanks for reply,
I have a method doBooking() which deal with lock,read,modify,write,unlock.
But I don't know where it should be, in server(like RemoteAccess or FactoryConnection) or in client?
How about you?
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
Originally posted by Ray Cheeny:

I have a method doBooking() which deal with lock,read,modify,write,unlock.
But I don't know where it should be, in server(like RemoteAccess or FactoryConnection) or in client?
How about you?

I have a facade in my client that does most of the stuff that is harder for the junior programmer to do... like bookFlight() which does the locking, modifying, etc.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ray,

But I don't know where it should be, in server(like RemoteAccess or FactoryConnection) or in client?

I put it in the client in my DataFacade class. That just seemed the most logical place.
Hope this help,
Michael Morris
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
if two clients attempt to perform the sequence lock, read, modify, write, unlock concurrently, then both modification attempts will be handled correctly.

Does that mean when one client is modify the record
,the other client could not just read the record?
I think it should be read,lock,...
Nate Johnson
Ranch Hand

Joined: May 13, 2002
Posts: 301
Originally posted by Ray Cheeny:

Does that mean when one client is modify the record
,the other client could not just read the record?
I think it should be read,lock,...

I think you are right... read, lock, read, modify, unlock is the way that most people do it. That way you can use the data that is in your model to start the booking (to do preliminary error checking before performing network traffic to the db), get a lock, then make sure you still have enough seats to continue booking.
[ August 26, 2002: Message edited by: Nate Johnson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused with singleton