BAEL-3838 Capturing a Java Thread Dump (#8780)

Co-authored-by: Somnath Musib <somnath.musib@voltbank.com.au>
This commit is contained in:
Somnath Musib 2020-03-07 04:12:06 +11:00 committed by GitHub
parent 40a17f0f63
commit 6600942cef
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.baeldung.threaddump;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ThreadDump {
public static void main(String[] args) throws IOException {
threadDump(true, true);
}
private static void threadDump(boolean lockedMonitors, boolean lockedSynchronizers) throws IOException {
Path threadDumpFile = Paths.get("ThreadDump.txt");
StringBuffer threadDump = new StringBuffer(System.lineSeparator());
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
for(ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) {
threadDump.append(threadInfo.toString());
}
Files.write(threadDumpFile, threadDump.toString().getBytes());
}
}