Click here to Skip to main content
15,911,139 members
Home / Discussions / Java
   

Java

 
GeneralRe: about java socket Pin
rualchina13-Oct-13 4:02
rualchina13-Oct-13 4:02 
GeneralRe: about java socket Pin
Richard MacCutchan13-Oct-13 4:53
mveRichard MacCutchan13-Oct-13 4:53 
GeneralRe: about java socket Pin
jschell14-Oct-13 8:00
jschell14-Oct-13 8:00 
QuestionImage transfer using TCP/IP Pin
Claraviolet10-Oct-13 5:22
Claraviolet10-Oct-13 5:22 
AnswerRe: Image transfer using TCP/IP Pin
Richard MacCutchan10-Oct-13 6:18
mveRichard MacCutchan10-Oct-13 6:18 
GeneralRe: Image transfer using TCP/IP Pin
Albert Holguin15-Oct-13 14:48
professionalAlbert Holguin15-Oct-13 14:48 
GeneralRe: Image transfer using TCP/IP Pin
Richard MacCutchan15-Oct-13 21:18
mveRichard MacCutchan15-Oct-13 21:18 
AnswerRe: Image transfer using TCP/IP Pin
Logical9416-Oct-13 19:04
professionalLogical9416-Oct-13 19:04 
QuestionAdding more than one JPanel to my JFrame(North,South) Pin
chdboy7-Oct-13 18:02
chdboy7-Oct-13 18:02 
SuggestionRe: Adding more than one JPanel to my JFrame(North,South) Pin
Richard MacCutchan7-Oct-13 21:31
mveRichard MacCutchan7-Oct-13 21:31 
GeneralRe: Adding more than one JPanel to my JFrame(North,South) Pin
chdboy9-Oct-13 1:13
chdboy9-Oct-13 1:13 
GeneralRe: Adding more than one JPanel to my JFrame(North,South) Pin
Shubhashish_Mandal9-Oct-13 3:15
professionalShubhashish_Mandal9-Oct-13 3:15 
GeneralRe: Adding more than one JPanel to my JFrame(North,South) Pin
chdboy9-Oct-13 19:04
chdboy9-Oct-13 19:04 
GeneralRe: Adding more than one JPanel to my JFrame(North,South) Pin
Richard MacCutchan9-Oct-13 4:24
mveRichard MacCutchan9-Oct-13 4:24 
GeneralRe: Adding more than one JPanel to my JFrame(North,South) Pin
chdboy9-Oct-13 19:03
chdboy9-Oct-13 19:03 
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 

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.