Making an interrupt back to a class
How would I make a class an interrupt back to its initializing class. I could pass this in as an initial argument, but I don't want want the memory use of passing in the whole object just so it can access one method.
I'm not quite sure on the names, but what I think I want is an abstract class that allows me to define my interrupt method in the class when I make a new instance of it.
Thanks for the help.
Here is the code of what I was trying to do.
Code :
public abstract class InteruptCreator extends Thread
{
private boolean go = true;
private int _time;
public InteruptCreator(int time)
{
_time = time;
start();
}
public void run()
{
while (go)
{
try
{
sleep(_time);
interupt();
}
catch(InterruptedException e){}
}
}
public abstract void interupt();
}
Code :
public class Main
{
private String _hostAddress; // address of web-server including file name on host
private int _updateTimeSecs; // amount of time between updates sent to web-server
private InteruptCreator _interupt;
public Main(String hostAddress, int updateTimeSecs)
{
_hostAddress = hostAddress;
_updateTimeSecs = updateTimeSecs;
// start interupt thread
_interupt = new InteruptCreator(6000, ??? new something @Override???);
}
}
Re: Making an interrupt back to a class
Quote:
I don't want want the memory use of passing in the whole object
Don't worry, only a reference to an existing object is passed, not the whole object.
Re: Making an interrupt back to a class
Quote:
but I don't want want the memory use of passing in the whole object just so it can access one method
Doing what you suggest uses next to no memory...java objects are passed as references (not as copies). Another tip: its better practice to implement Runnable rather than extending Thread
Re: Making an interrupt back to a class