Note that there are some explanatory texts on larger screens.

plurals
  1. POjstack output - get tid for java Thread.getId
    text
    copied!<p>I had a look at existing questions on getting the thread id like <a href="https://stackoverflow.com/questions/222108/getting-the-java-thread-id-and-stack-trace-of-run-away-java-thread">java-thread-id-and-stack-trace </a>.</p> <p>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.</p> <p>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.</p> <p>My questions are, take a sample jstack output:</p> <ol> <li><code>Event Batch Processing (Spring UAA/1.0.2)" daemon prio=10 tid=0x0000000041e27800 nid=0x363b waiting on condition [0x00007f9a89267000]</code> From this which id would be the <code>thread.getId()</code> as defined in <code>java.lang.Thread</code>? What is the last token in this line <code>[0x00007f9a89267000]</code>?</li> <li>How do I convert (Java code would be super) the <code>hex</code> to a Java <code>long</code>? </li> <li><p>Could you tell me why this did not work -> I took a hex <code>tid=</code> from a jstack of a thread that was hung in a never ending loop. I then did this:</p> <pre><code>Thread thrd = null; String thrdId =request.getParameter("thrd"); if(thrdId != null){ out.print("thread " + thrdId + " :"); out.flush(); Map&lt;Thread,StackTraceElement[]&gt; map = Thread.getAllStackTraces(); Set tt = map.keySet(); Iterator&lt;Thread&gt; ti = tt.iterator(); try{ long idFind = Long.parseLong(thrdId); out.print("&lt;br&gt; idFind thread |" + idFind + "| &lt;br&gt;"); boolean found = false; while(ti.hasNext() ){ thrd = ti.next(); if(thrd.getId() == idFind){ out.print("&lt;br&gt; stopping thread " + thrd + " &lt;br&gt;"); out.flush(); thrd.stop(); out.print("&lt;br&gt;stopped " ); found = true; break; } } if(!found){ out.print("&lt;br&gt;not found " ); } }catch(Exception e){ out.println("&lt;br&gt;err " + e + "&lt;br&gt;"); } } </code></pre></li> </ol> <p>Output of this was not found. The input param was the tid - pasted in Windows 7 calc, Programmer mode (hex) and then clicked Dec.</p> <p><strong>UPDATE:</strong></p> <p>In my test code when I made a JSP that loops infinitely, outputting a char every few 10000s iterations - then did a jstack, got:</p> <pre><code>"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... </code></pre> <p>I ran jstack a few times and get this entry every time.</p> <p>But when I run my JSP with thread <code>id = 000000000d4eb000</code>, it cant find it, since the app container names threads uniquely I was able to find the thread and when I print: </p> <pre><code>out.print("" + cnt + " \"" + thrd.getName() + "\"| id :" + thrd.getId() + "| hex :" + Long.toHexString(thrd.getId()) + "| &lt;br&gt;"); </code></pre> <p>I get :</p> <pre><code>93 "http-0.0.0.0-8181-4"| id :1389| hex :56d| 93 is the thread number in my internal counter in the loop. </code></pre> <p>So what jstack apparently identifies as finds as <code>0x000000000d4eb000</code> internal JVM knows as <code>1389 hex :56d</code>? 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload