Results 1 to 7 of 7
- 09-16-2009, 02:20 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
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();} catch (Exception e) {
retry = false;
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?
ThanksLast edited by coyote1982; 09-16-2009 at 09:10 PM.
- 09-16-2009, 03:33 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
What's the constructor code for your EventThread?
- 09-16-2009, 04:41 PM #3
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
- 09-16-2009, 09:07 PM #4
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
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.");} catch (IOException e) {
System.exit(1);
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.
- 09-17-2009, 07:52 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
I've found a way to do what I wanted. here it is, if anyone's interested:
do {
EventThread eventThread = new EventThread(serverIP, serverListeningPort);} while (true);
if (eventThread.getEventSocket() == null) {
System.out.println("Trying to reconnect...");}
continue;
eventThread.start();
- 09-17-2009, 09:21 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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:
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.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 outputs:
as I'd expect, since the exit(1) has killed the system before the start() is called.java.lang.Exception: Test exception
at dave.Test2$EventThread.<init>(Test2.java:51)
at dave.Test2.main(Test2.java:66)
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.
- 09-17-2009, 09:38 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
[SOLVED] Method from one thread called on another thread
By Ypsilon IV in forum Threads and SynchronizationReplies: 7Last Post: 04-24-2009, 02:07 PM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks