views:

174

answers:

4

When I call:

setChanged();
notifyObservers();

on a java.until.Observable class, will all the listening Observer objects complete execution of their udpate() methods - assuming we are running in the same Thread - before the java.until.Observable class continues running?

This is important because I will be sending a few messages through the notifyObservers(Object o) method in quick concession, it is important that each Observer class has finished its method before the new one though.

I understand that the order of execution for each Observer class may vary when we call notifyObservers() - it is just important that the order of method execution for each individual instance is in order.

A: 

Assuming that the notifyObservers()-method will notify all registered observers... yes... because your method calls are synchronous, i.e. the execution of your method will continue when the notifyObserver() method returns. The notifyObserver() returns when it has finished to iterate through all observers and call their update()-methods.

Simon
This is true and correct. It does not guarantee that asynchronous operations are not begun by the notifyObserver().
DJClayworth
+1  A: 

There is nothing asynchronous or threaded about Observable. I would suggest that if your update() methods are taking to long is that you have them put whatever information is needed in some sort of queue, and fire off a thread to pop the next task off the queue and do it. There are some nice thread safe queue structures that were introduced in Java 1.6 that should do the trick.

Paul Tomblin
+2  A: 

java.util.Observable makes many claims on what implementers are expected to do. However as an interface, none of this is enforced.

The contract for notifyObservers(Object o) is:

If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed. Each observer has its update method called with two arguments: this observable object and the arg argument.

There is not an expectation of this method being synchronized. This means that if you are calling notifyObservers(Object o) in different threads it should not be expected to be synchronized.

More important to your question, there is also not a corresponding expectation of java.util.Observer.update(Observable a, Object arg) finishing in a single thread. This means that you might call notifyObservers(Object o) all you like in a single thread, but Observer.update(Observable a, Object arg) might be spawning threads. If that is the case you cannot guarantee when the work it started will finish.

If you are writing both the Observers and the Observables and you are not spawning threads, you can be sure that each call to notifyObservers(Object o) will finish only after the last call to update(Observable o, Object arg) finishes.

Jacob Tomaw
A: 

"The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose." Your subclass should specify which choice was made. If it preserves the default, @Jacob Tomaw's analysis applies.

As an implementation detail, "The code where we extract each Observable from the Vector and store the state of the Observer needs synchronization, but notifying observers does not (should not)."

trashgod