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
posted
0
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
posted
0
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
posted
0
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
posted
0
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