• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: about lock/unlock concept

 
song bo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello.
when mutil-connection threads access the db file.should I lock the db file or the record of the file or both?I write the LockManager to test.I am not for sure.I am confused.please help me.
code:
class LockManager{
private HashMap hm = new HashMap();
public synchronized void lock(int recordNo) throws IOException{
Integer recObj = new Integer(recordNo);
Object obj = hm.get(recObj);
if(obj == null){
hm.put(recObj,new Object());
}else{
while( hm.get(recObj) != null) {
try
{
wait();
}catch(Exception e){
System.out.println(e.toString());
}
}
hm.put(recObj,new Object());
}
}

public synchronized void unlock(int recordNo){

Integer recObj = new Integer(recordNo);
Object obj = hm.get(recObj);
if( obj != null){
hm.remove(recObj);
notifyAll();
}

}
}
 
Billy Tsai
Ranch Hand
Posts: 1304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just lock the record is enough , there are plenty other posts about lock/unlcok in this forum u can refer to
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Song
Welcome to JavaRanch.
Billy is correct: there is no need to lock the file - all you need do is lock the record.
Your lock manager is a reasonable start. You will have to make quite a few modifications to the code before you could use it in this project, so I wont comment on it now (unless you want me to).
When you post code, could you please put it inside UBB code blocks? That way your code will still be indented. When you are editing your post, you will find buttons below the edit window that will help create the correct UBB tags for you.
Regards, Andrew
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi song,
As you don't store any additional information along with a lock, an implementation based on a HashSet would be clearer and enough IMO.
Otherwise, it seems perfect to me.
Best,
Phil.
[ August 04, 2003: Message edited by: Philippe Maquet ]
 
song bo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by all is reasonable.if only lock the file.it's not necessary for designing the "lockManager" class.thank your advices.
HashSet is good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic