how to kill a java thread by force?

J

john

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?
 
E

Eric Sosman

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

Lothar Kimmeringer

Eric said:
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: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Daniel Pitts

Lothar said:
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.
 
E

Eric Sosman

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?
 
R

Roedy Green

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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top