Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

"Asynchronous" Observer pattern in Java

Posted on 2002-07-10
10
Medium Priority
?
2,831 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 600 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

705 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