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