zno2

where do you come from ? (调试用打印堆栈信息和线程信息)

 

    void print() {
        StringBuffer sb = new StringBuffer();
        Thread currentThread = Thread.currentThread();
        sb.append("\n【Time】: ");
        sb.append(System.currentTimeMillis());
        sb.append("\n【Thread】:  id:");
        sb.append(currentThread.getId());
        sb.append(",name:");
        sb.append(currentThread.getName());
        sb.append("\n【StackTrace】:\n");
        StackTraceElement[] stackTrace = currentThread.getStackTrace();
        for (StackTraceElement stackTraceElement : stackTrace) {
            sb.append("  -");
            sb.append(stackTraceElement.getClassName());
            sb.append(".");
            sb.append(stackTraceElement.getMethodName());
            sb.append("(");
            sb.append(stackTraceElement.getFileName());
            sb.append(":");
            sb.append(stackTraceElement.getLineNumber());
            sb.append(")\n");
        }
        System.out.println(sb.toString());
    }

 

【Time】: 1496903193953
【Thread】:  id:1,name:main
【StackTrace】:
  -java.lang.Thread.getStackTrace(Thread.java:1479)
  -Foo.print(Foo.java:14)
  -FooTest.main(FooTest.java:6)

 

posted on 2023-08-25 16:30  zno2  阅读(10)  评论(0编辑  收藏  举报

导航