Skip to content

Commit 0d674b3

Browse files
musibsSomnath Musib
andauthored
BAEL-3838 Capturing a Java Thread Dump (eugenp#8780)
Co-authored-by: Somnath Musib <[email protected]>
1 parent 242c09f commit 0d674b3

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

  • core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.threaddump;
2+
3+
import java.io.IOException;
4+
import java.lang.management.ManagementFactory;
5+
import java.lang.management.ThreadInfo;
6+
import java.lang.management.ThreadMXBean;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
public class ThreadDump {
12+
13+
public static void main(String[] args) throws IOException {
14+
threadDump(true, true);
15+
}
16+
17+
private static void threadDump(boolean lockedMonitors, boolean lockedSynchronizers) throws IOException {
18+
Path threadDumpFile = Paths.get("ThreadDump.txt");
19+
20+
StringBuffer threadDump = new StringBuffer(System.lineSeparator());
21+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
22+
for(ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) {
23+
threadDump.append(threadInfo.toString());
24+
}
25+
Files.write(threadDumpFile, threadDump.toString().getBytes());
26+
}
27+
28+
}

0 commit comments

Comments
 (0)