In class "ParentClass", I create a number of other objects lets say "child_1" through "child_100". ParentClass implements " PropertyChangeListener". So when any child class has data to send back to the ParentClass, it executes firePropertyChange, and since ParentClass has registered itself as a listener, its propertyChange(PropertyChangeEvent evt) gets executed.
Sometimes, I have noticed that when many child_x fire, I tend to loose data. In other words, when child_1 has fired, ParentClass's "propertyChange" starts executing but before it has finished its job, another child lets say child_20 fires. So it appears that whatever task was supposed to have been done when child_1 fires, doesn't get done.
I hope I have explained my problem clearly. I guess, what I like to do is that access to ParentClass's "propertyChange" be not interrupted before it has finished its job. Could anyone suggest a technique? A simple example would be indeed greatly appreciated.
Well I have to assume each child is running in it own thread because if not then nothing will happen until after the property change listeners have completed. Even so if your code is thread safe you should not loose data - as another property firing your property change listener will not interupt the current one.
OK. So, the first question is, do you know what "synchronized" does, as in
The "synchronized" keyword means (in this context) that no two threads can invoke this method on the same object at the same time; so that if two events arrive one after the other, the second one will have to wait for the first call to complete before the second one begins.
It's not a magic bullet, though -- there's more to it than that. It's a big topic. Here is one page out of many in the Java Tutorial chapter on Threads which discusses the "synchronized" keyword as you'd want to use it here. Take a little time to read up on this stuff, then c'mon back and ask if'n you got any questions. [ September 27, 2006: Message edited by: Ernest Friedman-Hill ]
Thanks for advice. I use JBuilder and when I use the wizard to implement PropertyChangeListener it creates method "public void propertyChange(PropertyChangeEvent evt)" and is not with "synchronized". Offcourse I didn't even try adding "synchronized" and see if it compiles, before seeing your advice and it does.