• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3129
  • Last Modified:

"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.
0
mwalker
Asked:
mwalker
  • 2
  • 2
  • 2
  • +3
1 Solution
 
CEHJCommented:
Well, traditionally there are transactional implications here, which is why the Observers are in a Vector, no doubt, as thread safety is important. If, however, transactional issues are not important, you could use the better performing ArrayList to store Observers in your own implementation of Observable instead. Then you could spin off new Threads to notify each Observer. Hey presto - asynchronicity! But I won't get ahead of myself - at this point I'm just 'shooting from the hip'. But I might mull it over as I'm doing the washing up...
0
 
OviCommented:
put the notifyObservers() method call from code on a separate thread.
0
 
OviCommented:
Using events in Observer/Observable pattern meens that you destroy the meen of this pattern.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
heyhey_Commented:
> Has anyone out there thought along these lines or has implemented a similar asynchronous notification schema?


how do you define "asynchronous" ?
0
 
nir2002Commented:
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
0
 
Venci75Commented:
... or - the event handlers can create a new thread for processing the event.
0
 
mwalkerAuthor Commented:
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.
0
 
heyhey_Commented:
> 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".

0
 
CEHJCommented:
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) {}
}
0
 
Venci75Commented:
>> 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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now