Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting a stack trace for a Java ThreadInfo?

I have an application that calls getStackTrace() on a java.lang.management.ThreadInfo object, but the StackTraceElement array produced by the invocation is zero length.

Inspecting the Javadoc shows this (emphasis mine):

public StackTraceElement[] getStackTrace()

Returns the stack trace of the thread associated with this ThreadInfo. If no stack trace was requested for this thread info, this method will return a zero-length array. If the returned array is of non-zero length then the first element of the array represents the top of the stack, which is the most recent method invocation in the sequence. The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

How do I request a stack trace for this thread info?

like image 962
Greg Mattes Avatar asked Nov 30 '10 16:11

Greg Mattes


People also ask

How can I get the current stack trace in Java?

You can use Thread. currentThread(). getStackTrace() . That returns an array of StackTraceElement s that represent the current stack trace of a program.

How do I get a stack trace?

You can obtain a stack trace from a thread – by calling the getStackTrace method on that Thread instance. This invocation returns an array of StackTraceElement, from which details about stack frames of the thread can be extracted.

How do I print a full stack trace?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.


1 Answers

How are you calling getThreadInfo()?

Are you specifying a stack trace depth?

getThreadInfo(long id, int maxDepth) Returns a thread info for a thread of the specified id, with stack trace of a specified number of stack trace elements.

like image 124
David Gelhar Avatar answered Sep 28 '22 17:09

David Gelhar