• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

an issue with locking

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't use notify then the other threads may not be awakened when the lock is released.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Nothing up my sleeve ... and ... presto! A tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic