Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Making an interrupt back to a class

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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();
    }

    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???);
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an interrupt back to a class

    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.
    Last edited by Norm; June 1st, 2011 at 03:05 PM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Making an interrupt back to a class

    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

  4. #4
    Junior Member
    Join Date
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an interrupt back to a class

    Hmm, ok. Thank you.

Similar Threads

  1. making text editor using string class
    By petadeer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2011, 03:28 AM
  2. I'm back
    By Brt93yoda in forum The Cafe
    Replies: 3
    Last Post: November 15th, 2010, 10:29 PM
  3. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 01:50 PM
  4. Forgot to back up
    By WebPVP in forum Java Theory & Questions
    Replies: 5
    Last Post: October 13th, 2009, 07:13 PM