Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

newbee: use of notifyAll() etc  RSS feed

 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps the mother of stupid questions, but here it is:
consider an application that that stores data( a vector) by serialisation (on a floppy!).
void save(){
synchronized(vector){
//code to do the serialisation
}
}
Object getObject(int i){
synchronised(vector){
return vector.elementAt(i);
}
}
//etc
These methods are 'triggered" by different threads.
My question: is there any benefit by using wait() and/or notifyAll() in these methods? Does everything in this case happily automatic, as it seems to be?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should any of the threads wait until something happens? Should any of the threads notify any other threads when something happens? If not, then you have no use for wait() and notify().
Incidentally, I see a number of problems in the short snippet of code that you provided. First, Vector is a legacy collection and should not be used. Second, Vector is a synchronized collection and you synchronized around it. In some cases, that may be valid, but are you sure is that what you want? And finally, why not use a standard serialization mechanism for implementing serialization?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!