Results 1 to 7 of 7
  1. #1
    coyote1982 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default Retry constructing a thread/ terminate thread

    Hi,
    not sure this is a beginner's question...

    I have a class (which is called EventThread) that extends the Thread class and I want its constructor to try opening a Socket to some server. I would like to make it try and reconnect everytime it fails opening that Socket (when the server is unavailable, for example). I have surrounded the statement:
    new EventThread(serverIP, serverListeningPort).start();
    with a try block and a while block, like so:
    do {
    try {
    new EventThread(serverIP, serverListeningPort).start();
    retry = false;
    } catch (Exception e) {
    System.out.println("Trying to reconnect...");
    retry = true;
    }
    }
    while (retry);

    When the EventThread constructor fails to open a Socket it throws an IOException which is caught in a catch block. I thought that if I call the interrupt method from within that catch block, it would stop that thread and return to the class that invoked it (where the do-while block is), but instead EventThread continues to its run method and obviously throws an exception since it didn't establish a connection with the server.
    what's the right way to accomplish this?

    Thanks
    Last edited by coyote1982; 09-16-2009 at 09:10 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    What's the constructor code for your EventThread?

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Put all the code inside the run() method instead. if everything is in a loop, then it will try forever until you terminate it from outside the thread. Make sure after instantiation that you call the start() method on the thread to get it going :D

  4. #4
    coyote1982 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    What's the constructor code for your EventThread?
    here it is:
    public class EventThread extends Thread {
    private Socket eventSocket;

    public EventThread(String serverIP, int serverListeningPort) {
    super("EventThread");
    try {
    this.eventSocket = new Socket(serverIP, serverListeningPort);
    } catch (UnknownHostException e) {
    System.err.println("Unable to locate server.");
    System.exit(1);
    } catch (IOException e) {
    System.err.println("error occured while trying to connect to server on: " +
    serverIP + ":" + serverListeningPort);
    System.exit(1);
    }
    }
    }
    Last edited by coyote1982; 09-17-2009 at 09:35 AM.

  5. #5
    coyote1982 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    I've found a way to do what I wanted. here it is, if anyone's interested:

    do {
    EventThread eventThread = new EventThread(serverIP, serverListeningPort);
    if (eventThread.getEventSocket() == null) {
    System.out.println("Trying to reconnect...");
    continue;
    }
    eventThread.start();
    } while (true);

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    Your original idea is sort of OK, though you seem a bit confused as to when a thread is launched (not until start() is called).

    I don't quite understand, if an exception was thrown by new Socket(), why your app didn't simply shutdown. Take the following test code:

    Java Code:
    public class Test2 {
    	private static class EventThread extends Thread
    	{
    		public EventThread() {
    			super("EventThread");
    			try {
    				throw new Exception("Test exception");
    			} catch (Exception e) {
    				e.printStackTrace();
    				System.exit(1);
    			}
    		}
    		
    		@Override
    		public void run()
    		{
    			System.out.println("I'm running");
    		}
    
    	}
    	public static void main(String args[]) {
    		EventThread et = new EventThread();
    		et.start();
    		System.out.println("blah");
    	}
    }
    This does what yours does, assuming new Socket() throws an exception. I've simply taken out that bit and thrown an exception straight off the bat.

    This outputs:
    java.lang.Exception: Test exception
    at dave.Test2$EventThread.<init>(Test2.java:51)
    at dave.Test2.main(Test2.java:66)
    as I'd expect, since the exit(1) has killed the system before the start() is called.

    So I can only assume something else was going on in your code.

    By the way, rather than calling System.exit() I would have thrown an exception which the outer code would catch, rather than a check on null.

  7. #7
    coyote1982 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Default

    @Tolls

    Thanks for the reply...I did remove the System.exit() and it works perfectly

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 02:07 PM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •