Link to home
Start Free TrialLog in
Avatar of mwalker
mwalkerFlag for United States of America

asked on

"Asynchronous" Observer pattern in Java

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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ovi
Ovi

put the notifyObservers() method call from code on a separate thread.
Using events in Observer/Observable pattern meens that you destroy the meen of this pattern.
> Has anyone out there thought along these lines or has implemented a similar asynchronous notification schema?


how do you define "asynchronous" ?
Hi,

You can use JMS:
create topics, each one for specfic event.
Publish message to a topic, is creating specfic event.
Subscribers for specfic topic is to be listeners to specfic event.

This JMS offered solution provide you scalability and performance and is just waiting for you to use it, no need to recreate the wheel.

Best regards
Nir
... or - the event handlers can create a new thread for processing the event.
Avatar of mwalker

ASKER

Thank you all for you input. Here are some comments about your answers:

CEHJ: Your answer seems to work. I'm probably expand on this idea by using a ThreadPool instead of creating the threads as I need them.  The app does a lot of number crunching and I need this to be as efficient as possible.

heyhey: What I meant by "asynchronous" is to notify all of the observers to run their update() method nearly at the same time. For example, if an observable has 10 observers registered, the 10th one would not have to wait for the other 9 to finish before it runs.

Ovi: Placing the notifyObservers() method on a separate thread does remove its execution from the "main" thread. However, you still have the same situation that I explained above.

nir2002 and Venci75: JMS seems like a little over-kill for what I'm doing since all of the processing is being done on the same server.

Again, thank you all for your input.
> What I meant by "asynchronous" is to notify all of the observers to run their update() method nearly at the same time

so I suppose that the only problem is that update() method of some of the observers needs a lot of time to be executed ? in this situation Observer should do all it's processing in a separate Thread and it it the only responsible for this, i.e. it should start the Thread only if needed.

executing every update() in a separate Thread is a real over-kill. it's like situation in which button calls each ActionListener in a seperate Thread, so that they "can run their actionPerformed() method nearly at the same time".

Thanks mwalker. I would probably make the client call this (I'm sure you get the idea):

import java.util.Observable;
import java.util.Observer;

public class ObservableProxy extends Observable implements Observer {
  public void update(Observable o, Object arg) {}
}
>> nir2002 and Venci75: JMS seems like a little over-kill for what I'm doing since all of the processing is being done on the same server.

What I suggested is when you have an observer that needs a lot of time for executing the update() method is this observer to create its own thred