This discussion is archived
7 Replies Latest reply: Jan 18, 2012 7:17 AM by gimbal2 RSS

How can I stop a thread that do not support interruption?

910943 Newbie
Currently Being Moderated
The sendReceive method below is executed in the run method of a thread and is used to communicate with an HTTP server.
The method blocks until it receives a response back from the server.

How can I interrupt or stop the thread execution after a timeout period expired?
sendReceive is part of a third party library and do not support any timeout argument.


message = network.sendReceive();
  • 1. Re: How can I stop a thread that do not support interruption?
    gimbal2 Guru
    Currently Being Moderated
    Curious: did you actually try to call interrupt() on the thread?
  • 2. Re: How can I stop a thread that do not support interruption?
    EJP Guru
    Currently Being Moderated
    If the third-party library doesn't support timeouts, it is of negative value. It's part of the problem, not part of the solution. Throw it away and use HttpURLConnection, which does support timeouts.
  • 3. Re: How can I stop a thread that do not support interruption?
    910943 Newbie
    Currently Being Moderated
    I tested calling the thread interrupt method and it indeed interrupts the thread. Is this a guarantee that the thread will interrupt consistently though?
  • 4. Re: How can I stop a thread that do not support interruption?
    910943 Newbie
    Currently Being Moderated
    The third party API is the BlackBerry 6.0 API. I cannot just change it for something else!
  • 5. Re: How can I stop a thread that do not support interruption?
    gimbal2 Guru
    Currently Being Moderated
    hendre wrote:
    I tested calling the thread interrupt method and it indeed interrupts the thread. Is this a guarantee that the thread will interrupt consistently though?
    Java wouldn't be of much use to enterprises if it weren't reliable.
  • 6. Re: How can I stop a thread that do not support interruption?
    910943 Newbie
    Currently Being Moderated
    I bow to your superior knowledge my lord! However this is my understanding of thread interruption: Calling interrupt does not necessarily stop the target thread from doing what it is doing; it merely delivers the message that interruption has been requested. If I dot not explicitly know (as in have the source) if a third party blocking method handles interruption correctly, how can I be sure that it will interrupt each time I call interrupt and how can I be sure what the duration will be before the thread is interrupted. Calling interrupt is by no means a guarantee that a thread will stop its execution. My faith in Java is a strong as ever though!
  • 7. Re: How can I stop a thread that do not support interruption?
    gimbal2 Guru
    Currently Being Moderated
    hendre wrote:
    I bow to your superior knowledge my lord!
    No need for such snotty behavior; might there be a SLIGHT chance you misunderstand intentions?
    However this is my understanding of thread interruption: Calling interrupt does not necessarily stop the target thread from doing what it is doing; it merely delivers the message that interruption has been requested.
    You certainly read the javadocs correctly.
    If I dot not explicitly know (as in have the source) if a third party blocking method handles interruption correctly, how can I be sure that it will interrupt each time I call interrupt and how can I be sure what the duration will be before the thread is interrupted.
    you have just proven it to yourself. And because it worked this time, the next times you deal with the same API, framework and/or service you'll reliably get the same results. When you connect through a different API, framework or service you'll have to test it again of course, but I have faith in your abilities as a developer to reason that for yourself.
    Calling interrupt is by no means a guarantee that a thread will stop its execution.
    No, but my point was and is that in this particular case that you are describing you do have that guarantee, under the assumption that there are no other breaking points in your thread code. Until someone changes something, but you cannot really defend against that through code.
    My faith in Java is a strong as ever though!
    I would have more faith in your own intelligence and reasoning skills. Java is only a tool you can use to do great or terrible things.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points