• Post Reply Bookmark Topic Watch Topic
  • New Topic

Killing a java thread

 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I get a list of threads executing in JVM and then kill threads selectively. Is there any tool which helps me achieve this.

Another question was: If I have got a thread handle then how do I kill that thread. I read Thread.stop is deprecated.
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaj Kumarkk wrote:
Another question was: If I have got a thread handle then how do I kill that thread. I read Thread.stop is deprecated.


The reason that the stop() method has been deprecated is because it is not possible to guarantee to kill a thread in a safe manner. The stop() method will kill the thread, but it can have bad side effects, like take JVM locks with it. The stop() method works, if you want to kill a thread, use the stop() method.

The only way to guarantee to kill a thread safely, is to do it cooperatively -- the killing thread, and the thread to be killed have to work together. You have to have the thread to be killed, be able to exit gracefully, track notifications or interruptions. And the killing thread has to work with it -- it can be as simple as setting a flag if the thread to be killed is just doing compute work, or as complex as setting flags, interrupting the thread, closing files and sockets, etc., if the thread to be killed is doing I/O on a machine that don't support I/O interrupts.

Henry

 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaj Kumarkk wrote:Can I get a list of threads executing in JVM and then kill threads selectively. Is there any tool which helps me achieve this.


If the JVM JMX container is configured for remote monitoring, one option is to get to the thread mxbean. This mbean however, only allows monitoring -- I don't think that there is any way to kill a thread with it.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!