• Post Reply Bookmark Topic Watch Topic
  • New Topic

Terminating Threads

 
Sanjit Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am not too familiar with thread programming and thus, any help with my problem would be highly appreciated.
I have a program that wants to contact a server, but sometimes it hangs in the middle. Thus, I have tried to use threads to implement timeout.
I start two threads, one of them goes to sleep for a particular period of time and the other one starts the process of contacting the server. If the contact is successfull, but the sleeping thread is still alive, then no timeout occurs. But on the other hand, if the sleeping thread wakes up and the contact thread is still executing, then I would like to "terminate" that thread and simply show Timeout message.
I have imeplemented something, but it doesn�t work properly. Sample code is as follows:
******************************************
public synchronized void performOperation(String request, int sequence) {
Thread timerThread = new TimerThread();
Thread contactXMLClient = new ContactXMLClient(request, sequence);

contactXMLClient.start();
timerThread.start();
while(contactXMLClient.isAlive() && timerThread.isAlive()) {
//simply wait.dont do anything
}
if(contactXMLClient.isAlive()) {
contactXMLClient.interrupt();//interrupt is only a possibility
System.out.println("TIMEOUT");
}
else {
timerThread.interrupt();
System.out.println("Worked");}
}
***********************************************
//private classes
private class TimerThread extends Thread {
public TimerThread(){}

public void run() {
try {
this.sleep(20000);
} catch(Exception e) {}
}
}

private class ContactXMLClient extends Thread {
private String xml;
private int sequence;

public ContactXMLClient(String xml, int sequence) {
this.xml = xml;
this.sequence = sequence;
}

public void run() {
String line;
String receivedXML = "";
try {
URL repURL = new URL(url);
HttpURLConnection con = (HttpURLConnection) repURL.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
byte[] bytes = xml.getBytes();
con.setRequestProperty("Content-length", String.valueOf(bytes.length));

OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

while((line = in.readLine()) != null)
receivedXML = receivedXML + "\n" + line;

in.close();
con.disconnect();
} catch(Exception e) {}
}}
*********************************************
the interrupt method was only a suggestion. I hope you understand what I want to achieve. The above does not work properly and any help would be highly appreciated. Thanks!
 
Michael Bronshteyn
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of calling interupt you can try different approach.
1. If timer thread expires, set a flag to indicate that it actually expired.
2. Have your xml client post and read data in the loop, having some reasonably small chunks of data. Within that loop you can check if timer thread set "expiration" flag. If so, have xml client thread return from run method.
Of course that would work only if your xml client thread is not hanged due to tcp/ip connection problems and actually executes through the code.
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lines

Won't terminate until both threads are already done -- interrupt() is never being called!
Also, I suggest you remove the timer thread entirely and just put the sleep method directly in performOperation(). (Actually I don't suggest that. I suggest you rethink you're approach. See the rest of this post)
If you really want to wait for the threads to die, it's better to use Thread.join() than while (Thread.isAlive()).
Here's one approach that I think works (if BufferedReader.read() doesn't block, as I believe):

[ November 29, 2002: Message edited by: David Weitzman ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an old discussion, but my code was wrong. BufferedReader blocks if no input is available but not when it runs out of available input after a few chars. The loop needs to be like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!