Solved

"Asynchronous" Observer pattern in Java

Posted on 2002-07-10
10
2,595 Views
Last Modified: 2007-11-27
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
Comment
Question by:mwalker
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 7144203
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
 
LVL 9

Expert Comment

by:Ovi
ID: 7145292
put the notifyObservers() method call from code on a separate thread.
0
 
LVL 9

Expert Comment

by:Ovi
ID: 7145295
Using events in Observer/Observable pattern meens that you destroy the meen of this pattern.
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 7145353
> Has anyone out there thought along these lines or has implemented a similar asynchronous notification schema?


how do you define "asynchronous" ?
0
 
LVL 2

Expert Comment

by:nir2002
ID: 7145376
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:Venci75
ID: 7145415
... or - the event handlers can create a new thread for processing the event.
0
 

Author Comment

by:mwalker
ID: 7146113
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 7146138
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 7146144
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
 
LVL 9

Expert Comment

by:Venci75
ID: 7146146
>> 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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Issues installing SSL certificate into Apache Tomcat 3 70
array11 challenge 16 52
strCopies  challenge 17 73
Java Timer (static) 9 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now