Stopping a Thread blocking on I/O

Hi,

Does anyone have a simple, elegant and clean solution for stopping a Thread that is blocking on an I/O call in an MIDP client.

My problem is that any of the Java I/O calls could block while sending a request or receiving a response but the user must have the ability to Stop his current request at any time.

Any help please.

Many Thanks,

Vinesh.

My code is as follows :

public class MessageHandler extends Thread

{

private static Communication comms = null;

........

........

public void run()

{

try

{

........

........

// Do Some Processing of the Input data here

........

........

// Send Request / Receive Response

byte bOut[] = null;

bOut = comms.DoServerCall(bInData);

........

........

// Do Some Processing of the Output data here

........

........

}

........

........

}

}

public class Communication

{

private String url;

........

........

public Communication(String url)

{

this.url = url;

}

public byte [] DoServerCall(byte request[]) throws IOException

{

HttpConnection conn = null;

InputStream in = null;

OutputStream out = null;

byte response[] = null;

// Open Connection

conn = (HttpConnection)Connector.open(url);

........

........

// Send Request

out = conn.openOutputStream();

out.write(request);

out.flush();

// Receive Response

int rc = conn.getResponseCode();

........

........

in = conn.openInputStream();

int lengthResponse = (int) conn.getLength();

........

........

response = IOUtil.BufferedByteRead(in, lengthResponse, url);

// BufferedByteRead method just reads the entire response in a while loop

// while (remaining > 0)

// {

//int bytesRead = in.read(bReceived, offset, remaining);

//........

//........

// }

........

........

return response;

}

}

[2189 byte] By [vineshp01] at [2007-9-26 11:17:54]
# 1
How about running it non-blocking, return control to the user interface (i.e. the Cancel button), and when a request comes in your callback will be invoked and you process the data - or if the user clicks Cancel, you can cancel the call.Don
d1camero at 2007-7-2 0:21:46 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2
Don - how do you set it to "non-blocking"?thanks,-J
jchalfan at 2007-7-2 0:21:46 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Good point. When I wrote the note, I was not aware that you could not set socket options. I also cannot find where IO calls cannot be set to non-blocking. I also checked if the IO call could be put in a seprate thread, then cancel the thread, but no luck there either.

Your original request seems to suggest that you do have a "non-elegant" method. Can you post it and maybe someone can figure out something a bit more elegant.

Don

d1camero at 2007-7-2 0:21:46 > top of Java-index,Java Mobility Forums,Java ME Technologies...