Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill a java thread using VisualVM or using a unix command?

Tags:

I m using windows 7 OS. I have around 6 threads in my application. For the purpose of testing the alerts to check the health of the threads, i need to kill the threads manually and check if the alerts are working properly. Can we kill a thread like how we kill a process with its pid?

like image 661
Arun Avatar asked Jul 23 '12 10:07

Arun


People also ask

How do you kill a thread in Unix?

tgkill() sends the signal sig to the thread with the thread ID tid in the thread group tgid. (By contrast, kill(2) can only be used to send a signal to a process (i.e., thread group) as a whole, and the signal will be delivered to an arbitrary thread within that process.)

How can I kill thread in Java?

Modern ways to suspend/stop a thread are by using a boolean flag and Thread. interrupt() method. Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say 'exit'. Whenever we want to stop a thread, the 'exit' variable will be set to true.

Which method kills the thread?

A thread is automatically destroyed when the run() method has completed. But it might be required to kill/stop a thread before it has completed its life cycle. Previously, methods suspend(), resume(), and stop() were used to manage the execution of threads.

How do you kill a blocked thread in Java?

You can try to call Thread. stop() or Thread.


1 Answers

Dan Woods documented how to kill a thread in this blog entry... https://web.archive.org/web/20160302023213/http://www.rhcedan.com/2010/06/22/killing-a-java-thread The steps he performed involved using a debugger (JDB) and injecting an exception in the thread's execution. Specifically...

  1. Ensure that your java program is started with the following parameters:

    -Dcom.sun.management.jmxremote.port=50199
    -Dcom.sun.management.jmxremote.authenticate=false
    -Dcom.sun.management.jmxremote.ssl=false
    -Xrunjdwp:transport=dt_socket,address=50100,server=y,suspend=n

    This will allow us to attach the java debugger to the running process, after we identify which Thread is causing the problem. Also, make sure that you have your iptables setup appropriately so as to only allow connections on 50100 and 50199 from the hosts/workstations that you manage.

  2. Identify the offending thread:
  3. Kill the thread. In this example, the ThreadName is “btpool0-0?. Fire up the java debugger (also shipped with the JDK distribution), and attach to the running JVM…

    [root@host ~]# jdb -attach 50100

Get a list of the running threads — this will also give us the thread id as the JVM sees it:

> threads   --snip--   (org.mortbay.thread.BoundedThreadPool$PoolThread)0x25cb btpool0-0 running   --snip-- 

The thread id that we’re going to kill is “0x25cb”. The first step of killing the thread is to jump into it, and suspend it…

thread 0x25cb btpool0-0[1] suspend 0x25cb btpool0-0[1] step Step completed: <... snip ...> btpool0-0[1] kill 0x25cb new java.lang.Exception() killing thread: btpool0-0 btpool0-0[1] instance of  com.site.package.name(name='btpool0-0', id=9675) killed btpool0-0[1] 

Exit the java debugger, and you’re done!

like image 132
buzz3791 Avatar answered Sep 25 '22 03:09

buzz3791