"Asynchronous" Observer pattern in Java
Posted on 2002-07-10
I've looked at the code in the java.util.Observable class and I saw that the Observable stores its Observers in a Vector and in the notifyObservers() method, it iterates through the vector (with a for loop) to notify each of the Observers. This is OK if performance of each of the Observer's update() method is not an issue.
However, I have a situation where I am concerned about the performers of the Observer's update() method. So, I thought that an asynchronous notification would be in order (i.e. all Observers will be notified near the same time). I also thought of creating some kind of event-driven system where the Observer would register itself as a listener to an event fired by the Observable (similar to event handling in Swing). My only issue is that I'm not writing a GUI so I didn't know how to do this without the AWT Event Thread.
Has anyone out there thought along these lines or has implemented a similar asynchronous notification schema? I'd appreciate some design guidance (code would be even better). Thanks in advance for your help.