10

It is possible to kill a thread that is in state RUNNING in a non programatically way?

I know that top command in *nix can show threads. Can I kill the thread in the OS?

I'd like to know if there is a way to link a thread to a process so I can kill only that specific thread and not the application.

We had a bug in our code that kept a thread in state RUNNING in a synchronized method. The thread kept the lock on the object "hanging" the application.

The bug is fixed. But I wonder if is possible.

4
  • 2
    if you have a pid you can kill it! Nov 18, 2011 at 10:12
  • looking for solution only on *nix or not? Nov 18, 2011 at 10:13
  • ProcessExplorer on windows can kill individual threads, so yes, it is possible. probably on unix too. Nov 18, 2011 at 10:20
  • @MarekSebera: preferibly not OS specific. But a *nix solutions is fine!
    – ssedano
    Nov 18, 2011 at 10:43

3 Answers 3

13

The short answer is "maybe, but you should not and most of the time it won't work either".

The long answer is:

"Maybe..." Some JVM implementation map java threads to OS threads and some do not. If the JVM does a mapping to a native OS thread, you might be able to kill that thread with some process tool that the OS provides (like kill on *nix). If the JVM does green threads, meaning it doesn't map a Java thread to an OS level thread, then you are basically out of luck using OS level tools. Luckily only very few JVM implementations do this. An approach that can be used regardless in which way the JVM organizes it's threads, is using the java debugger. This article describes the procedure of doing it: http://www.rhcedan.com/2010/06/22/killing-a-java-thread/.

"but you should not do it" Killing a thread on the OS level will almost certainly leave the JVM in an undefined state (read "jvm might crash or delete all files on your disk or do whatever it fricking pleases to do"). Even when going the debugger way, only a very small amount of java applications (read "no application made on this planet") will properly handle the event that an outside application is killing one of it's threads. As a result these applications will be put in an undefined state (read "application might crash or delete all files on your disk or do whatever it fricking pleases to do").

"and most of the time it won't work either" If the thread is really stuck with some blocked IO etc, then killing the thread won't work, it will just not respond. If a program is stuck it's probably better to kill the whole program, find the issue with the program and fix it instead of killing a single thread.

1
  • 1
    I know it is not the way. Is just wondering. Thanks for your answer.
    – ssedano
    Nov 18, 2011 at 11:03
2

For all your doubts on killing a thread, refer this: http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html

1
  • 1
    The poster asked for a "non-programmatic" way of doing this.
    – Jan Thomä
    Nov 18, 2011 at 10:35
1

On linux, there is a tkill(int tid, int sig) command, similar to kill.

On windows, ProcessExplorer can do it from gui, don't know if there is anything with cli.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.