当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Thread dumpStack()用法及代码示例


线程类的 dumpStack() 方法将当前线程的堆栈跟踪打印到标准错误流。它仅用于调试。

用法

public static void dumpStack()

返回

此方法不返回任何值。

示例

public class JavaDumpStackExp 
{
    public static void main(String[] args) 
    {
        Thread thread = Thread.currentThread();
        thread.setName("My ThreadDumpStack");

        // set thread priority to 6
        thread.setPriority(6);
        // prints the current thread
        System.out.println("Current thread:" + thread);
    
        int count = Thread.activeCount();
        System.out.println("currently active threads:" + count);

        // prints a stack trace of the current thread to the standard error stream, used for debugging 
        Thread.dumpStack();
   }
}

输出:

Current thread:Thread[My ThreadDumpStack,6,main]
currently active threads:1
java.lang.Exception:Stack trace
	at java.lang.Thread.dumpStack(Thread.java:1336)
	at javatpoint.java.JavaDumpStackExp.main(JavaDumpStackExp.java:19)






相关用法


注:本文由纯净天空筛选整理自 Java Thread dumpStack() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。