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 an issue with locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "an issue with locking" Watch "an issue with locking" New topic
Author

an issue with locking

amit sharma777
Greenhorn

Joined: May 25, 2001
Posts: 11
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

Joined: Apr 03, 2001
Posts: 178
if you don't use notify then the other threads may not be awakened when the lock is released.
Conor Allen
Ranch Hand

Joined: Apr 27, 2001
Posts: 32
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Apr 27, 2001
Posts: 32
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
 
Don't get me started about those stupid light bulbs.
 
subject: an issue with locking
 
Similar Threads
FBN: criteriaFind(String criteria) method; is this ok ?
Remote Objects Interface
havin problems with Object array
NullPointerException
implementing database lock