Solved

"Asynchronous" Observer pattern in Java

Posted on 2002-07-10
10
2,620 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 41
thymeleaf natural templating vs JSP 2 68
ForLoop Example 3 40
even odd program using while loop 3 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

920 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

15 Experts available now in Live!

Get 1:1 Help Now