Click here to Skip to main content
15,888,158 members
Home / Discussions / Java
   

Java

 
QuestionProblem On SQL Server driver Configuration in JDeveloper Pin
AMADIAR3-Oct-13 5:38
AMADIAR3-Oct-13 5:38 
SuggestionRe: Problem On SQL Server driver Configuration in JDeveloper Pin
Richard MacCutchan3-Oct-13 21:26
mveRichard MacCutchan3-Oct-13 21:26 
SuggestionRe: Problem On SQL Server driver Configuration in JDeveloper Pin
Prasad Khandekar8-Oct-13 4:07
professionalPrasad Khandekar8-Oct-13 4:07 
QuestionUsing the Observer and Observable Class Pin
Ryan Little30-Sep-13 16:26
Ryan Little30-Sep-13 16:26 
AnswerRe: Using the Observer and Observable Class Pin
Shubhashish_Mandal30-Sep-13 22:31
professionalShubhashish_Mandal30-Sep-13 22:31 
QuestionRe: Using the Observer and Observable Class Pin
Ryan Little1-Oct-13 3:11
Ryan Little1-Oct-13 3:11 
AnswerRe: Using the Observer and Observable Class Pin
Shubhashish_Mandal1-Oct-13 3:38
professionalShubhashish_Mandal1-Oct-13 3:38 
GeneralRe: Using the Observer and Observable Class Pin
Ryan Little1-Oct-13 7:29
Ryan Little1-Oct-13 7:29 
I have been using java2s for all my questions on this problem.

Updated Code: (Use the original posted code for classes not posted here)
Java
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
Java
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 CAction()
		{
			public void doAction() throws InterruptedException{
				Thread.sleep(TIMEOUT);
				if (Value.getItsValue() > 20 && Value.getItsValue() <= 40)
				{
					Value.setItsValue(Value.getItsValue() + 2);
				}
			}
		});
		Value.addObserver(AddTwo);
		
		AddTen = new CData();
		AddTen.setItsAction(new CAction(){
			public void doAction() throws InterruptedException{
				Thread.sleep(TIMEOUT);
				if (Value.getItsValue() > 40 && Value.getItsValue() <= 80)
				{
					Value.setItsValue(Value.getItsValue() + 10);
				}
			}
		});
		Value.addObserver(AddTen);
		
		AddSeven = new CData();
		AddSeven.setItsAction(new CAction(){
			public void doAction() throws InterruptedException{
				Thread.sleep(TIMEOUT);
				// Added a maximum limit on this so when 100 is reached
				// the re-calculation process is halted until further notice
				if (Value.getItsValue() > 80 && Value.getItsValue() <= 100)
				{
					Value.setItsValue(Value.getItsValue() + 7);
					
				}
			}
		});
		Value.addObserver(AddSeven);
		
		MaxValueReached = new CData();
		MaxValueReached.setItsAction(new CAction()
		{
			public void doAction() throws InterruptedException{
				Thread.sleep(TIMEOUT);
				if (Value.getItsValue() >= 100)
				{
					System.out.print("Maximum Value Reached\n");
				}
			}
		});
		Value.addObserver(MaxValueReached);
		Value.startBehavior();
		
		// This was while(true)'
		while (true)
		{
			if (Value.getItsValue() >= 100)
			{
				Value.setItsValue(0);
			}
		}
	}
}

Again, thanks for the feedback
GeneralRe: Using the Observer and Observable Class Pin
Shubhashish_Mandal2-Oct-13 22:26
professionalShubhashish_Mandal2-Oct-13 22:26 
GeneralRe: Using the Observer and Observable Class Pin
angrybobcat7-Oct-13 20:55
angrybobcat7-Oct-13 20:55 
QuestionSVM implementation in JAVA Pin
Member 1030559829-Sep-13 18:07
Member 1030559829-Sep-13 18:07 
AnswerRe: SVM implementation in JAVA Pin
jschell30-Sep-13 8:33
jschell30-Sep-13 8:33 
QuestionGet & Set Pin
Shuvo Sarker29-Sep-13 10:32
professionalShuvo Sarker29-Sep-13 10:32 
AnswerRe: Get & Set Pin
bryce29-Sep-13 13:14
bryce29-Sep-13 13:14 
AnswerRe: Get & Set Pin
Richard MacCutchan29-Sep-13 21:18
mveRichard MacCutchan29-Sep-13 21:18 
AnswerRe: Get & Set Pin
Logical9416-Oct-13 19:07
professionalLogical9416-Oct-13 19:07 
QuestionGetting value from an eventListener? Pin
KatsuneShinsengumi28-Sep-13 3:31
KatsuneShinsengumi28-Sep-13 3:31 
AnswerRe: Getting value from an eventListener? Pin
Richard MacCutchan28-Sep-13 6:46
mveRichard MacCutchan28-Sep-13 6:46 
GeneralRe: Getting value from an eventListener? Pin
KatsuneShinsengumi28-Sep-13 18:47
KatsuneShinsengumi28-Sep-13 18:47 
GeneralRe: Getting value from an eventListener? Pin
Richard MacCutchan28-Sep-13 21:19
mveRichard MacCutchan28-Sep-13 21:19 
GeneralRe: Getting value from an eventListener? Pin
KatsuneShinsengumi28-Sep-13 21:26
KatsuneShinsengumi28-Sep-13 21:26 
Questiontell me java code Pin
Member 1030137427-Sep-13 9:30
Member 1030137427-Sep-13 9:30 
AnswerRe: tell me java code Pin
NotPolitcallyCorrect27-Sep-13 9:36
NotPolitcallyCorrect27-Sep-13 9:36 
QuestionNeper number Pin
parisa6821-Sep-13 5:23
parisa6821-Sep-13 5:23 
AnswerRe: Neper number Pin
Richard MacCutchan21-Sep-13 5:56
mveRichard MacCutchan21-Sep-13 5:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.