天天看点

java print thread id,jstack输出-获取Java Thread.getId的tid

java print thread id,jstack输出-获取Java Thread.getId的tid

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 the thread.getId() as defined in java.lang.Thread? What is the last token in this line [0x00007f9a89267000]?

How do I convert (Java code would be super) the hex to a Java long?

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 map = Thread.getAllStackTraces();

Set tt = map.keySet();

Iterator ti = tt.iterator();

try{

long idFind = Long.parseLong(thrdId);

out.print("

idFind thread |" + idFind + "|

");

boolean found = false;

while(ti.hasNext() ){

thrd = ti.next();

if(thrd.getId() == idFind){

out.print("

stopping thread " + thrd + "

");

out.flush();

thrd.stop();

out.print("

stopped " );

found = true;

break;

}

}

if(!found){

out.print("

not found " );

}

}catch(Exception e){

out.println("

err " + e + "

");

}

}

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()) + "|

");

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?

解决方案

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.