Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused with singleton

 
Ray Cheeny
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ray Cheeny
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can anybody help me with the above question?
 
Nate Johnson
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ray Cheeny
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nate,
congratulations!!!
Another question.How to implement the lock(-1) and when to invoke it?
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic