I have been using java2s for all my questions on this problem. Updated Code: (Use the original posted code for classes not posted here)
public class CData implements Observer
{
public CData()
{
// Do Nothing
}
@Override
public void update(Observable arg0, Object arg1) {
if (itsAction != null)
{
try {
this.itsAction.doAction();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private CAction itsAction = null;
public CAction getItsAction() {
return itsAction;
}
public void setItsAction(CAction itsAction) {
this.itsAction = itsAction;
}
}
public class CValue extends Observable
{
private double itsValue = 0;
public double getItsValue() {
return itsValue;
}
public void setItsValue(double itsValue) {
if (this.itsValue != itsValue)
{
this.itsValue = itsValue;
System.out.print("Value set to " + itsValue + "\\n");
setChanged();
notifyObservers();
}
}
public void startBehavior()
{
setChanged();
notifyObservers();
}
}
Observation: When I execute setChanged and then execute notifyObservers, the test program does a re-calculation on the new data. If the re-calculation doesn't change the value, then no new notifications are required. If there is a change in data from the re-calculation then the process is started all over again. I made a small modification to CMain to stop the notifications after a period of time and restart the re-calculation process again. This is eliminating the memory leak completely. From these new revelations, attached is my new CMain code
public class CMain {
private final static int TIMEOUT = 5;
// Public data...Could be in the form of a singleton.
private static CValue Value;
private static CData AddFive;
private static CData AddTwo;
private static CData AddTen;
private static CData AddSeven;
private static CData MaxValueReached;
public static void main(String\[\] args) {
// All the data variables which will be used
Value = new CValue();
AddFive = new CData();
AddFive.setItsAction(new CAction()
{
@Override
public void doAction() throws InterruptedException {
Thread.sleep(TIMEOUT);
if (Value.getItsValue() <= 20)
{
Value.setItsValue(Value.getItsValue() + 5);
}
}
});
Value.addObserver(AddFive);
AddTwo = new CData();
AddTwo.setItsAction(new CActi