Termination of a Thread?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sanjit_Singh
Posted On:   Wednesday, November 27, 2002 01:44 AM

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 d   More>>

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 + "
" + 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!

   <<Less

Re: Termination of a Thread?

Posted By:   Laurent_Mihalkovic  
Posted On:   Wednesday, November 27, 2002 12:20 PM

You may want to read the following for more information about threading in Java.

  • Some interesting links
  • Doug Lea's site for his book on Java threading. He is as expert as it gets in the field. In particular, he has an interesting presentation on the nio package (java 1.4)

cheers

Re: Termination of a Thread?

Posted By:   Simon_Ablett  
Posted On:   Wednesday, November 27, 2002 05:52 AM

I don't have time to go through your code in detail but one problem that is immediately visible is that the 'while' loop in your main thread is going to chew up all of your CPU cycles and probably starve the child threads. TYry adding a sleep or a wait clause in there.

You could always get rid of your timer thread and the while loop in the main thread and simply have something akin to the following.

contactXMLClient.start();
try
{
Thread.sleep(50000);
}
catch(InterruptedException ie )
{
}

if (contactXMLClient.isAlive()==true)
{
contactXMLClient.interrupt();
}

This will interrupt your child thread if it is still running after 50 seconds. Alternatively set the child to a daemon thread and then the main thread won't wait for it to complete before exiting. Another technique would be to have the main thread set a status variable in the child when it wants it to die and then in the child's while loop just check the status of this variable.

Regards.
About | Sitemap | Contact