File tree Expand file tree Collapse file tree
core-java-modules/core-java-perf/src/main/java/com/baeldung/threaddump Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments