From: john on
in my project, sometimes some java thread can't be killed immediately,
is there any good ways to slove the problem?
in java API Thread.java, there isn't stop method, whether it means in
java, App writer can't kill a thread forcely or there is some around
methods?
From: Peter Duniho on
john wrote:
> in my project, sometimes some java thread can't be killed immediately,
> is there any good ways to slove the problem?
> in java API Thread.java, there isn't stop method, whether it means in
> java, App writer can't kill a thread forcely or there is some around
> methods?

Don't write a thread that you need to kill forcefully. Always provide
some mechanism by which your code in the thread can exit gracefully and
in a predictable way.

Corallary: don't use third-party code that also does not provide this
feature in its code.

For related information, see:
http://java.sun.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

Pete
From: Eric Sosman on
On 12/14/2009 4:05 AM, john wrote:
> in my project, sometimes some java thread can't be killed immediately,
> is there any good ways to slove the problem?
> in java API Thread.java, there isn't stop method, whether it means in
> java, App writer can't kill a thread forcely or there is some around
> methods?

If the victim thread isn't cooperating (by checking a
"stop now" flag occasionally or some such), the only way I
can think of to stop it safely is System.exit().

The problem with hurling a hand grenade into a thread's
vitals at some arbitrary moment is that you don't know what
the thread was doing at the moment you blew it up. If it
was in the middle of updating a data structure somewhere, the
data structure may now be half-updated, useless or even
poisonous to the rest of the program. You've got to make sure
the victim is in a "safe" state at the moment of termination,
and you can't do that without the victim's cooperation.

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Lothar Kimmeringer on
Eric Sosman wrote:

> On 12/14/2009 4:05 AM, john wrote:
>> in my project, sometimes some java thread can't be killed immediately,
>> is there any good ways to slove the problem?
>> in java API Thread.java, there isn't stop method, whether it means in
>> java, App writer can't kill a thread forcely or there is some around
>> methods?
>
> If the victim thread isn't cooperating (by checking a
> "stop now" flag occasionally or some such), the only way I
> can think of to stop it safely is System.exit().

System.exit waits for all non-daemon-threads to stop.
So if the Thread you want to kill is not a daemon-thread
and it ignores all attempts to stop, you're screwed.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang(a)kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
From: Daniel Pitts on
Lothar Kimmeringer wrote:
> Eric Sosman wrote:
>
>> On 12/14/2009 4:05 AM, john wrote:
>>> in my project, sometimes some java thread can't be killed immediately,
>>> is there any good ways to slove the problem?
>>> in java API Thread.java, there isn't stop method, whether it means in
>>> java, App writer can't kill a thread forcely or there is some around
>>> methods?
>> If the victim thread isn't cooperating (by checking a
>> "stop now" flag occasionally or some such), the only way I
>> can think of to stop it safely is System.exit().
>
> System.exit waits for all non-daemon-threads to stop.
> So if the Thread you want to kill is not a daemon-thread
> and it ignores all attempts to stop, you're screwed.
Actually, barring a security exception, System.exit forcefully exits the
JVM.

Without a call to system.exit, non-daemon threads will keep the JVM alive.


--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>