使用 notifyObservers() 的观察者通知顺序
Order of Observer notification with notifyObservers()
我有以下代码:
public class MyObservable extends Observable {
// ...
public void doSomething() {
// do stuff
setChanged();
notifyObservers();
}
}
public class A implements Observer {
public void update(Observable o, Object arg) {
// do something
}
}
public class B implements Observer {
public void update(Observable o, Object arg) {
// do something
}
}
主要功能:
public static void main(String[] args) {
MyOvervable a = new MyObservable();
a.addObserver(new A());
a.addOberser(new B());
a.doSomething();
}
notifyObservers()
调用更新函数的顺序与我用addObserver()
添加观察者的顺序相同吗?
是的。这是摘自 java java.util.Observable
class.
private Vector<Observer> obs;
arrLocal = obs.toArray();
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
Vector
是基于索引的,并由内部数组支持。 Vector
维护元素的插入顺序。意味着您可以假设如果迭代一个 Vector,您将按照插入的顺序获得对象。
所以你的问题的答案是:是
根据 Oracle 的 docs:
The order in which notifications will be delivered is unspecified. 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.
Observable 的 Javadoc 说通知是按照注册侦听器的顺序进行的,但这是不正确的 - 通知实际上是按 相反的顺序。
带有解释性注释的代码摘录:
// addObserver() adds last-most at end of the list
public synchronized void addObserver(Observer o) {
..
obs.addElement(o);
}
public void notifyObservers(Object arg) {
..
// notifyObservers iterates backwards; last-most first.
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
我提交了一个错误报告 -- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8214760 -- 但是由于 Observable 被弃用,它已被关闭为 'will not fix'。
我相信实施的行为是稳定的,不会改变;更改顺序可能会破坏许多使用它的应用程序的兼容性。
我有以下代码:
public class MyObservable extends Observable {
// ...
public void doSomething() {
// do stuff
setChanged();
notifyObservers();
}
}
public class A implements Observer {
public void update(Observable o, Object arg) {
// do something
}
}
public class B implements Observer {
public void update(Observable o, Object arg) {
// do something
}
}
主要功能:
public static void main(String[] args) {
MyOvervable a = new MyObservable();
a.addObserver(new A());
a.addOberser(new B());
a.doSomething();
}
notifyObservers()
调用更新函数的顺序与我用addObserver()
添加观察者的顺序相同吗?
是的。这是摘自 java java.util.Observable
class.
private Vector<Observer> obs;
arrLocal = obs.toArray();
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
Vector
是基于索引的,并由内部数组支持。 Vector
维护元素的插入顺序。意味着您可以假设如果迭代一个 Vector,您将按照插入的顺序获得对象。
所以你的问题的答案是:是
根据 Oracle 的 docs:
The order in which notifications will be delivered is unspecified. 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.
Observable 的 Javadoc 说通知是按照注册侦听器的顺序进行的,但这是不正确的 - 通知实际上是按 相反的顺序。
带有解释性注释的代码摘录:
// addObserver() adds last-most at end of the list
public synchronized void addObserver(Observer o) {
..
obs.addElement(o);
}
public void notifyObservers(Object arg) {
..
// notifyObservers iterates backwards; last-most first.
for (int i = arrLocal.length-1; i>=0; i--)
((Observer)arrLocal[i]).update(this, arg);
}
我提交了一个错误报告 -- https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8214760 -- 但是由于 Observable 被弃用,它已被关闭为 'will not fix'。
我相信实施的行为是稳定的,不会改变;更改顺序可能会破坏许多使用它的应用程序的兼容性。