对我来说,当前的执行线程是当前线程的主线程。如果要转储所有线程,请使用以下命令:
public static String getDumpFor(Thread thread) {
StringBuilder st = new StringBuilder();
if (thread.isAlive()) {
StackTraceElement[] stackTrace = thread.getStackTrace();
st.append(thread.toString()).append("\n")
.append(String.format(" State - %s,", thread.getState()))
.append(String.format(" Is daemon = %s,", thread.isDaemon()));
for (StackTraceElement s : stackTrace)
st.append("\tat ").append(s.getClassName()).append(".").append(s.getMethodName()).append("(").append(s.getFileName()).append(":").append(s.getLineNumber()).append(")")
.append("\n");
} else {
st.append(" No thread ");
}
return st.toString();
}
/**
* print thread dump for each active thread over logger API.
*/
public static void dumpActiveThreads() {
Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
Set<Thread> keySet = stackTraces.keySet();
System.out.println("\nThread dump begin:");
for (Thread thread : keySet)
dumpActiveThread(thread);
System.out.println("\nThread dump end.");
}
但是我仍然可能误解了“如何获得真正的线程名称?”这个问题......