jstack output - get tid for java Thread.getId

眉间皱痕 提交于 2019-12-13 05:58:44

问题


I had a look at existing questions on getting the thread id like java-thread-id-and-stack-trace .

But I'm not able to figure out something that seemed simple. I want to make a JSP tool to stop a thread in Java.

I know it's a bad idea in general but we need it as we can't use JConsole in our environment cause some hardening of JBoss.

My questions are, take a sample jstack output:

  1. Event Batch Processing (Spring UAA/1.0.2)" daemon prio=10 tid=0x0000000041e27800 nid=0x363b waiting on condition [0x00007f9a89267000] From this which id would be the thread.getId() as defined in java.lang.Thread? What is the last token in this line [0x00007f9a89267000]?
  2. How do I convert (Java code would be super) the hex to a Java long?
  3. Could you tell me why this did not work -> I took a hex tid= from a jstack of a thread that was hung in a never ending loop. I then did this:

    Thread thrd = null;
    String thrdId =request.getParameter("thrd");
    if(thrdId != null){
        out.print("thread " + thrdId + " :");
        out.flush();
        Map<Thread,StackTraceElement[]> map = Thread.getAllStackTraces();
        Set tt = map.keySet();
        Iterator<Thread> ti = tt.iterator();
        try{
            long idFind = Long.parseLong(thrdId);
            out.print("<br> idFind thread |" + idFind + "| <br>");
            boolean found = false;
            while(ti.hasNext() ){
                thrd = ti.next();
                if(thrd.getId() == idFind){
                    out.print("<br> stopping thread " + thrd + " <br>");
                    out.flush();
                    thrd.stop();
                    out.print("<br>stopped " );
                    found = true;
                    break;
                }
            }
            if(!found){
                out.print("<br>not found " );
    
            }
        }catch(Exception e){
            out.println("<br>err " + e + "<br>");
        }
    
    }
    

Output of this was not found. The input param was the tid - pasted in Windows 7 calc, Programmer mode (hex) and then clicked Dec.

UPDATE:

In my test code when I made a JSP that loops infinitely, outputting a char every few 10000s iterations - then did a jstack, got:

"http-0.0.0.0-8181-4" daemon prio=6 tid=0x000000000d4eb000 nid=0x2f58 waiting on condition [0x0000000010fee000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)

...(stack trace)
at org.apache.jsp.t.testing.loop_jsp._jspService(loop_jsp.java
...more stack trace...

I ran jstack a few times and get this entry every time.

But when I run my JSP with thread id = 000000000d4eb000, it cant find it, since the app container names threads uniquely I was able to find the thread and when I print:

out.print("" + cnt + " \"" + thrd.getName() + "\"| id :" + thrd.getId() + "| hex :" +  Long.toHexString(thrd.getId()) + "| <br>");

I get :

93 "http-0.0.0.0-8181-4"| id :1389| hex :56d| 

93 is the thread number in my internal counter in the loop.

So what jstack apparently identifies as finds as 0x000000000d4eb000 internal JVM knows as 1389 hex :56d? Finally changed the JSP to search by name and stop the thread. lucky the thread names are all different. But is this a bug - running Windows, JBoss 4.2, Java 1.6 update 32?


回答1:


You can only find and stop your own threads. I would try interrupting the thread in case it can stop gracefully.

Stopping a thread is a bad idea in general as it can leave the application in an inconsistent or unrecoverable state. The only way you can avoid this is to have a very good understand of exactly everything the thread might be doing, in what case its likely that fixing the code so it stops gracefully is simpler and better

we cant use jconsole in our environment

jvisualvm is a much better tool in any case. I wouldn't use jconsole unles syou had to.

how do I convert (java code would be super) the hex to a java long?

long idFind = Long.parseLong(thrdId, 16);

could you tell me why this did not work

Either you didn't run this from the same process or the thread caught the ThreadDeath error you triggered.



来源:https://stackoverflow.com/questions/12258589/jstack-output-get-tid-for-java-thread-getid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!