Name: bsT130419 Date: 10/12/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, interpreted mode)
I would inherited vom java.util.Observable and would ensure, that every call to
notifyObservers() notifies all registered observers without all explizit call to
setChanged(). In the documentation was written "If this object has changed, as
indicated by the hasChanged method, then notify all of its observers and
then...". So I overwrote the hasChanged() method in this way:
public boolean hasChanged() { return true; }
But no go... A call to setChanged() is still needed. It seems, that notifyAll()
doesn't use hasChanged().
The question is: Is the documenation wrong, the implementation or I?
workaround : I wrote a small wrapper around notifyAll():
public void myNotify(ShepHerdEvent event)
{
setChanged();
notifyObservers(event);
}
Here are my source file to reproduce it:
package org.of.bug;
import java.util.Observable;
import java.util.Observer;
public class ObserverDemo
{
public static void main(String[] args)
{
Spy spy = new Spy();
SpyVictimI victim1 = new SpyVictimI();
SpyVictimII victim2 = new SpyVictimII();
victim1.addObserver(spy);
victim2.addObserver(spy);
System.out.println("-[ DEMO START ]-");
victim1.doit();
victim2.doit();
System.out.println("-[ DEMO END ]-");
}
static class SpyVictimII extends Observable
{
public void doit()
{
notifyObservers();
}
public String toString()
{
return "SpyVictimII";
}
public boolean hasChanged()
{
return true;
}
}
static class Spy implements Observer
{
public void update(Observable o, Object arg)
{
System.out.println("Spy: Was informed by " + o.toString());
}
}
static class SpyVictimI extends Observable
{
public void doit()
{
setChanged();
notifyObservers();
}
public String toString()
{
return "SpyVictimI";
}
}
}
(Review ID: 133651)
======================================================================
- duplicates
-
JDK-8025885 Observable doesn't check HasChanged() method to notify Observers
-
- Closed
-