Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement thread timeout in J2ME?

I am using Connector in J2ME and found that sometimes it take a very long time to send a request, and sometime it may halt the whole process.
So I try to put code related to Connector in a thread and try to set timeout to the thread. But I found out that it's very difficult to do this in J2ME because there's no join(timeout) in J2ME.
Is there any way to do this? Or any better way to deal with network connection in J2ME?
Thanks a lot in advance.

like image 882
jagttt Avatar asked Aug 16 '26 20:08

jagttt


1 Answers

You should use a combination of 3 mechanisms:

  • Connector.open() can be used with a third parameter for timeout.
  • You can call Thread.sleep() and Connection.close() from a different thread than the one you call Connector.open() in.
  • You can call Thread.sleep() and Thread.interrupt() from a different thread than the one you call Connector.open() in.

On some badly implemented MIDP runtimes, Connection.close() can block if the connection is busy. You need to test this thoroughly on the handsets you plan to support.

Thread.interrupt() can leave the MIDP runtime (and the underlying handset operating system) with orphaned resources so only use it as a last resort.

like image 98
michael aubert Avatar answered Aug 18 '26 11:08

michael aubert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!