快速业务通道

Java线程运行栈信息的获取 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-19
kTraceElement = false; private static Method getStackTrace = null; private static Method getClassName = null; private static Method getFileName = null; private static Method getMethodName = null; private static Method getLineNumber = null; …. // 以下代码省略

可以看到,Log4J 1.3仍然兼容JDK1.3,而且为JDK1.4也做了相应的优化。

Java线程运行栈信息的获取(4)

时间:2011-02-13

五、JDK1.5的Thread Stack Trace

JDK1.5在Thread类里面引入了getStackTrace()和getAllStackTraces()两个方法。这下子,我们不用 (new Throwable()).getStackTrace ();可以调用

Thread.getCurrentThread().getStackTrace()来获得当前线程的运行栈信息。不仅如此,只要权限允许,还可以获得其它线程的运行栈信息。

/** * Returns an array of stack trace elements representing the stack dump * of this thread. This method will return a zero-length array if * this thread has not started or has terminated. * If the returned array is of non-zero length then the first element of * the array represents the top of the stack, which is the most recent * method invocation in the sequence. The last element of the array * represents the bottom of the stack, which is the least recent method * invocation in the sequence. * * If there is a security manager, and this thread is not * the current thread, then the security managers * checkPermissionmethod is called with a * RuntimePermission("getStackTrace")permission * to see if its ok to get the stack trace. * * Some virtual machines may, under some circumstances, omit one * or more stack frames from the stack trace. In the extreme case, * a virtual machine that has no stack trace information concerning * this thread is permitted to return a zero-length array from this * method. * * @return an array of StackTraceElement, * each represents one stack frame. * * @since 1.5 */ public StackTraceElement[] getStackTrace() { if (this != Thread.currentThread()) { // check for getStackTrace permission SecurityManager security = System.getSecurityManager(); if (security != null) { security.checkPermission( SecurityConstants.GET_STACK_TRACE_PERMISSION); } } if (!isAlive()) { return EMPTY_STACK_TRACE; } Thread[] threads = new Thread[1]; threads[0] = this; StackTraceElement[][] result = dumpThreads(threads); return result[0]; } /** * Returns a map of stack traces for all live threads. * * @since 1.5 */ public static Map getAllStackTraces() { // check for getStackTrace permission // Get a snapshot of the list of all threads }

六、总结

从总的发展趋势来看,JDK不仅提供越来越多、越来越强的功能,而且暴露给用户的控制方法越来越多,越来越强大。

文章来源:http://blog.csdn.net/buaawhl/archive/2005/01/18/257653.aspx

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号