I'm looking at writing moni开发者_如何学Pythontoring 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.
Have you tried Thread.getAllStackTraces()
Take a look at ThreadMXBean.dumpAllThreads(boolean, boolean)
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] info = bean.dumpAllThreads(true, true);
Have you looked into the methods of the Thread class such as Thread.enumerate()
, Thread.activeCount()
, Thread.getName()
?
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.
kill -SIGQUIT {java_process}
will dump all stack traces in the stdout of the java process.
精彩评论