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:
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 thethread.getId()
as defined injava.lang.Thread
? What is the last token in this line[0x00007f9a89267000]
?- How do I convert (Java code would be super) the
hex
to a Javalong
? 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?