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

Java

 
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 
I am writing a small data driven application which takes data from an input and throw an event throughout the program that is subscribed to the data event listener. Currently, I have the small test program setup as an infinite loop to see if there are any memory leaks. Testing has shown that there is a massive memory leak and I don't know what to do. I have the program attached:

What is happening is when the event is handled, it is not properly disposed of. Instead, it is thrown onto the stack while a new event is serviced. I thought sleeping the current event would dispose of the last event but...nope! I am out of ideas on how to properly service and dispose of events quickly using this form. Any ideas would be helpful to figure this small technical issue out. Thanks - Ryan

The Main loop which has the data items and the main value observer.

Java
import java.util.*;

public class CMain {

	private final static int TIMEOUT = 250;
	
	// 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);
				if (Value.getItsValue() > 80)
				{
					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)
				{
					Value.setItsValue(0);
					System.out.print("Value Reset back to 0\n");
				}
			}
		});
		Value.addObserver(MaxValueReached);
		Value.startBehavior();
		
		while (true);
	}
	
	
}


The CValue class

Java
import java.util.Observable;

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");
				System.out.print(false);
				setChanged();
				notifyObservers();
			}
		}
		
		public void startBehavior()
		{
			setChanged();
			notifyObservers();
		}
	}


The CData Class

Java
import java.util.Observable;
import java.util.Observer;

public class CData extends Observable 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;
	}
}


And to explain what this CAction thing is:

Java
public interface CAction {
	public void doAction() throws InterruptedException;
}

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 
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 

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.