> For example, the function is called getCompressedPrices(). It
> occasionally fails to return anything and freezes my application.
[quoted text clipped - 5 lines]
>
> Enter
>> Hi,
>>
[quoted text clipped - 5 lines]
> thread) sleep for a certain time and check the variable. If it didn't
> finish, terminate the thread.
Which raises the very interesting question: how do you safely terminate a
method or thread that is running code you cannot modify? Of course, if the
method is designed to be halted just follow its directions. I don't think
that's generally the case, however. Interrupt() typically only applies to
code that is waiting. Stop() and suspend () are deprecated and can leave
the system in an invalid state. They might work, but you won't know whether
they're safe.
Destroy () isn't implemented (according to Javadocs), which means it has the
same effect as ignoring the thread, but doing so allows potentially
deadlocked or CPU-bound code to consume resources or to incur further
problems with new tasks that are created (e.g. the broken code is holding
locks needed by additional requests.)
Knowing more about the code itself may lead to a technique just for that
code, like closing a socket or something, but generally there are no
techniques for safely stopping 3rd party code. If it really has to be safe,
put it in a separate process.
Cheers,
Matt Humphrey http://www.iviz.com/
Roedy Green - 09 Nov 2007 00:07 GMT
>Which raises the very interesting question: how do you safely terminate a
>method or thread that is running code you cannot modify?
you can't. The method to do it is deprecated. If you try to kill it
gently, the killed thread must co-operate. If it has gone insane, it
may be in on position to co-operate.
see com.mindprod.common11.StoppableThread bundled in
http://mindprod.com/products1.html#COMMON11

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Are Nybakk - 09 Nov 2007 14:18 GMT
>>> Hi,
>>>
[quoted text clipped - 12 lines]
> the system in an invalid state. They might work, but you won't know whether
> they're safe.
Yes I noticed the deprecations. That was new to me. I see the API links
to a page which describes some alternative methods to halt a thread:
http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDe
precation.html
> Destroy () isn't implemented (according to Javadocs), which means it has the
> same effect as ignoring the thread, but doing so allows potentially
[quoted text clipped - 9 lines]
> Cheers,
> Matt Humphrey http://www.iviz.com/
Matt Humphrey - 09 Nov 2007 17:16 GMT
>>>> Hi,
>>>>
[quoted text clipped - 17 lines]
>
> http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDe
precation.html
Those notes show how to design or refactor a method or thread so that it can
be stopped, but if you already have a method that is not well-behaved and
cannot be rewritten you're out of luck. Wrapping a non-stoppable method
with one that is stoppable will not produce a stoppable method.
Matt Humphrey http://www.iviz.com/