File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes newbee: use of notifyAll() etc Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "newbee: use of notifyAll() etc" Watch "newbee: use of notifyAll() etc" New topic
Author

newbee: use of notifyAll() etc

Ko Wey
Ranch Hand

Joined: Sep 08, 2003
Posts: 67
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

Joined: Oct 08, 2001
Posts: 2937
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbee: use of notifyAll() etc
 
Similar Threads
newbee with JTable
Synchronization-lock-unlock
Doubt about Serialization
Java file / Java bean
Synchronization Question