Re: Speaking of thread safety?
Knute Johnson <nospam@rabbitbrush.frazmtn.com> wrote in news:MkRln.9227
$NH1.2316@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();}
}