• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized revisted...

 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Like many of you, I am using a HashMap to hold my record locks.
private Map lockTable = Collections.synchronizedMap(new HashMap());
My question is: can I simply lock the table by making the 'lock' and 'unlock' methods of the Data class 'synchronized'? Seems to me that is the cleanest solution provided it is not too restrictive.
thanks in advance,
Max
 
Sajid Raza
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If lock and unlock are marked as synchronized you would potentially block access to other synchronized methods. I suppose its a small point.
More importantly, as long as you're recording the existence of the lock-request in the lock method, blocking when a lock already exists, and notifying in unlock I don't see any problems with the approach.
[This message has been edited by Sajid Raza (edited August 01, 2001).]
 
Devu Shah
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Cognate for such great score.
Can you guide me in locking.
I have simply used Hashmap as it is thread self.
R_Lock = new HashMap(); // THIS IS FROM ANOTHER CLASS
��..
��

synchronized(R_Lock){
while(R_Lock.containsKey(new Integer(recno))){
try {
R_Lock.wait();
BUT to my surprise everyone in this group uses :
private Map lockTable = Collections.synchronizedMap(new HashMap());

Can you guide me where am I wrong? Is not Hashmap threadsafe ?
Or my design is all right.?
Thanks in advance.
devu
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Devu
No HashMap is not threadsafe, a Hashtable is but that is old 1.1 stuff, eventhough I guess you cold still use it. In order to make a HashMap threadsafe you have to use
Map lockTable = Collections.synchronizedMap(new HashMap());
Or also some Synchronized blocks could do the trick also
Trevor

[This message has been edited by Trevor Dunn (edited August 02, 2001).]
 
Sajid Raza
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used a HashMap to store lock-info. A HashMap is not thread safe. However, once you limit access to the HashMap as you have in synchronized blocks that attempt to gain exclusive ownership of the HashMap's monitor you've assured thread safe access (as in your code).
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make HashMap threadsafe with a call to the Collections
framework e.g.
private Map lockTable = Collections.synchronizedMap(new HashMap());
BTW, I tested out using this with the lock and unlock methods coded as synchronized and it works with multiplw clients, not blocking for calls using different record numbers.
IMHO this is the most challenging part of the assignment.
thanks for all the assistance,
max
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic