justinhj
justinhj

Reputation: 11306

Can you suspend a Java app and get a snapshot of it's threads from within the app?

I'm looking at writing monitoring code that runs inside a Java application and periodically takes a snapshot of running threads and displays some call stack information on each.

Ideally this would be extended for profiling and/or monitoring.

I don't want to use an external tool, as this is for self educational purposes.

Upvotes: 3

Views: 198

Answers (5)

Nick Hristov
Nick Hristov

Reputation: 915

kill -SIGQUIT {java_process}

will dump all stack traces in the stdout of the java process.

Upvotes: 0

corsiKa
corsiKa

Reputation: 82579

You could register the threads you want to watch at creation time, and have a separate metrics thread to do the monitoring. You would want to build certain metrics into the thread, such as maybe a list of recent running times, current throughput, or other sorts of metrics.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533530

Have you tried Thread.getAllStackTraces()

Upvotes: 4

Mark Peters
Mark Peters

Reputation: 81074

Take a look at ThreadMXBean.dumpAllThreads(boolean, boolean)

ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] info = bean.dumpAllThreads(true, true);

Upvotes: 3

JRL
JRL

Reputation: 78003

Have you looked into the methods of the Thread class such as Thread.enumerate(), Thread.activeCount(), Thread.getName() ?

Upvotes: 1

Related Questions