Prev: Exception Handling
Next: First class developer: who ?
From: Knute Johnson on 10 Mar 2010 13:15 Does anybody know if the Observable/Observer calls to update() are on the EDT? Thanks, -- Knute Johnson email s/nospam/knute2010/
From: markspace on 10 Mar 2010 13:34 Knute Johnson wrote: > Does anybody know if the Observable/Observer calls to update() are on > the EDT? Considering that Observer is just an interface and it would depend on the implementation, and that no Java Swing classes that I know of actually implement Observer, I'd have to guess the answer is in general "no." If you mean the Swing method update( Graphics ), that isn't related to the Observer interface. All Swing methods are not thread safe unless noted otherwise, and update(Graphics) isn't an exception, so I think you can assume that any caller has done their homework and called it on the EDT. Although if you are implementing it yourself, putting in an assertion might be a wise idea. assert SwingUtilities.isEventDispatchThread();
From: Knute Johnson on 10 Mar 2010 14:11 On 3/10/2010 10:34 AM, markspace wrote: > Knute Johnson wrote: >> Does anybody know if the Observable/Observer calls to update() are on >> the EDT? > Considering that Observer is just an interface and it would depend on > the implementation, and that no Java Swing classes that I know of > actually implement Observer, I'd have to guess the answer is in general > "no." The Observable class method, notifyObservers() is called and that then calls all Observer.update() methods. I'm curious to know if the Observable puts those calls onto the EDT. -- Knute Johnson email s/nospam/knute2010/
From: markspace on 10 Mar 2010 14:49 Knute Johnson wrote: > On 3/10/2010 10:34 AM, markspace wrote: >> Knute Johnson wrote: >>> Does anybody know if the Observable/Observer calls to update() are on >>> the EDT? > >> Considering that Observer is just an interface and it would depend on >> the implementation, and that no Java Swing classes that I know of >> actually implement Observer, I'd have to guess the answer is in general >> "no." > > The Observable class method, notifyObservers() is called and that then > calls all Observer.update() methods. I'm curious to know if the > Observable puts those calls onto the EDT. > Again, since Observable has NO connection to Swing at all, I see no reason that a rational person reading its Java doc would conclude that Observable had anything to do with the EDT, Swing, or the price of tea in China. You do realize that Sun's Java doc shows all inheriting classes for any given class, right? There are no classes at all in Sun's API that extend Observable (read the Java doc for it), it's just a class for the Observer Pattern than you can use yourself. I think some classes may use Observable internally, but that's different: private inheritance (Java style, via composition). Go read the Java docs of the other class, Observable doesn't do anything at all except what it says.
From: Ian Shef on 10 Mar 2010 15:57
Knute Johnson <nospam(a)rabbitbrush.frazmtn.com> wrote in news:MkRln.9227 $NH1.2316(a)newsfe14.iad: > Does anybody know if the Observable/Observer calls to update() are on > the EDT? > > Thanks, > In java 1.6.0_17, NO, unless notifyObservers(...) is called on the EDT. However, there is no guarantee, and the Javadoc specifically states that subclasses of Observable may even deliver notifications on separate threads. The following sample program prints 1.6.0_17 Thread[main,5,main] Thread[AWT-EventQueue-0,6,main] for me, demonstrating that the update() calls are on the thread used for notifyObservers(...). Your Java may be different. Here is the sample program: package testpack1; import java.util.*; import javax.swing.SwingUtilities; public class OberverTest { static void showThread(Observable1 o1) { o1.change(); o1.notifyObservers(); } public static void main(String[] args) { // Create an Observer that prints the Thread used for update(...) Observer observer = new Observer() { @Override public void update(Observable arg0, Object arg1) { System.out.println(Thread.currentThread()) ; } } ; final Observable1 observable = new Observable1(); observable.addObserver(observer); System.out.println(System.getProperty("java.version")); showThread(observable); // Notify from the main thread. SwingUtilities.invokeLater(new Runnable() { @Override public void run() { showThread(observable); // Notify from the EDT. } }); } } // Define a minimally useful Observable class Observable1 extends Observable { public void change() {setChanged();} } |