获取当前线程堆栈
程序员文章站
2022-07-05 13:21:25
...
public static String getThreadStack(Thread currentThread) {
StackTraceElement[] stackTraceElementArray = currentThread.getStackTrace();
StackTraceElement locationStackTraceElement = stackTraceElementArray[10];
String locationString = String.format(" @%s.%s()", locationStackTraceElement.getClassName(),
locationStackTraceElement.getMethodName());
StringBuilder builder = new StringBuilder();
builder.append(getThreadTitle(currentThread)).append("\n").append(locationString).append("\n");
int skip = 11;
for (int index = skip; index < stackTraceElementArray.length; index++) {
StackTraceElement ste = stackTraceElementArray[index];
builder.append(" at ")
.append(ste.getClassName())
.append(".")
.append(ste.getMethodName())
.append("(")
.append(ste.getFileName())
.append(":")
.append(ste.getLineNumber())
.append(")\n");
}
return builder.toString();
}