Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to kill a java thread by force?

204 views
Skip to first unread message

john

unread,
Dec 14, 2009, 4:05:17 AM12/14/09
to
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?

Peter Duniho

unread,
Dec 14, 2009, 4:17:12 AM12/14/09
to

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

Eric Sosman

unread,
Dec 14, 2009, 8:16:08 AM12/14/09
to

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
eso...@ieee-dot-org.invalid

Lothar Kimmeringer

unread,
Dec 14, 2009, 12:22:08 PM12/14/09
to
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: spam...@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!

Daniel Pitts

unread,
Dec 14, 2009, 12:51:34 PM12/14/09
to
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/>

Eric Sosman

unread,
Dec 14, 2009, 6:20:37 PM12/14/09
to
On 12/14/2009 12:22 PM, Lothar Kimmeringer wrote:
> Eric Sosman wrote:
> [...]

>> 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.

Are you sure? The Javadoc for System and for Runtime
don't mention any such thing. There's lots about shutdown
hooks and finalizers and recursive calls to exit(), but
nothing about waiting for other threads.

Hmmm: If you're right, this program will never terminate:

public class RunForever implements Runnable {

public static void main(String[] unused) {
demonic();
new Thread(new RunForever()).start();
stall(3500);
System.exit(0);
}

public void run() {
demonic();
for (;;) {
stall(1000);
}
}

private static void demonic() {
Thread self = Thread.currentThread();
System.err.println(self.getName()
+ (self.isDaemon() ? " is " : " is not ")
+ "a daemon");
}

private static void stall(long time) {
String name = Thread.currentThread().getName();
try {
Thread.sleep(time);
System.err.println(name
+ " awoke at " + System.currentTimeMillis());
} catch (InterruptedException ex) {
System.err.println(name
+ " interrupted at " + System.currentTimeMillis());
}
}
}

On my machine, it terminates as expected. Bug in my Java?

--
Eric Sosman
eso...@ieee-dot-org.invalid

Roedy Green

unread,
Dec 14, 2009, 6:32:51 PM12/14/09
to
On Mon, 14 Dec 2009 01:05:17 -0800 (PST), john
<junzha...@gmail.com> wrote, quoted or indirectly quoted someone
who said :

The only safe way to do it is to have the thread check a boolean
periodically and commit suicide if true.

--
Roedy Green Canadian Mind Products
http://mindprod.com
The future has already happened, it just isn�t evenly distributed.
~ William Gibson (born: 1948-03-17 age: 61)

0 new messages