File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an issue with locking

 
amit sharma777
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not tried to use wait or notify in my lock method but instead i've synchronized on the record I've locked.It seems to work fine please see code:
The variable v here is the vector containing the records.
public void lock(int record) {
try{
DataInfo uu=getRecord(record);
if((v.size()!=0)){
for(int i=0;i<v.size();i++){>
if ((v.size()==0)| |(!(((DataInfo)v.elementAt(i)).getValues()[0]).equals(getRecord(record).getValues()[0]))){
synchronized(uu){
v.addElement(uu);
}}
}
}
else{
synchronized(uu){
v.addElement(uu);
System.out.println(v.contains(uu));
}
}
}
catch(DatabaseException q){
System.out.println(q.getMessage());
}
}
please give me some suggestions on this code .
 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you don't use notify then the other threads may not be awakened when the lock is released.
 
Conor Allen
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm .... not sure what is going in the for loop ... however it is possible for things to change between the for loop and the syncronize block - I have a feeling that this might not be thread safe. Also how does this block other threads from aquiring a lock and continuing to modify the locked record?
Anyway, just my two pence worth
Conor
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is certainly not threadsafe. If you've got a little brother, you must have noticed that
   if (see(icecream)) { eat(icecream); }
does not prevent the little b@$#! from butting in and snatching away the ice cream before you get a chance to eat it.
- Peter
 
Conor Allen
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have a look at some of the locking strategies suggested in this discussion group ... It might give you some food for thought.
Conor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic