views:

76

answers:

5

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.

+1  A: 

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

JRL
+2  A: 

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

ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] info = bean.dumpAllThreads(true, true);
Mark Peters
+4  A: 

Have you tried Thread.getAllStackTraces()

Peter Lawrey
I do not believe there is a way to stop all threads (from within the Java byte code). If you launch the JVM itself using JNI, there _might_ be a way.
Justin
thanks that is good for my purposes
justinhj
+1  A: 

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.

glowcoder
A: 

kill -SIGQUIT {java_process}

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

Nick Hristov