java.lang.Thread.getStackTrace()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(5.9k)|赞(0)|评价(0)|浏览(330)

本文整理了Java中java.lang.Thread.getStackTrace()方法的一些代码示例,展示了Thread.getStackTrace()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.getStackTrace()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:getStackTrace

Thread.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, has started but has not yet been scheduled to run by the system, 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 manager's checkPermission method is called with a RuntimePermission("getStackTrace") permission to see if it's 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.
[中]返回表示此线程堆栈转储的堆栈跟踪元素数组。如果此线程尚未启动、已启动但尚未计划由系统运行或已终止,则此方法将返回零长度数组。如果返回的数组长度非零,那么数组的第一个元素表示堆栈的顶部,这是序列中最近的方法调用。数组的最后一个元素表示堆栈的底部,它是序列中最近的方法调用。
如果存在安全管理器,且该线程不是当前线程,则会使用RuntimePermission(“getStackTrace”)权限调用安全管理器的checkPermission方法,以查看是否可以获取堆栈跟踪。
在某些情况下,某些虚拟机可能会从堆栈跟踪中忽略一个或多个堆栈帧。在极端情况下,不具有与此线程相关的堆栈跟踪信息的虚拟机允许从此方法返回零长度数组。

代码示例

代码示例来源:origin: netty/netty

@Override
public StackTraceElement[] stackTrace() {
  return t.getStackTrace();
}

代码示例来源:origin: redisson/redisson

@Override
public StackTraceElement[] stackTrace() {
  return t.getStackTrace();
}

代码示例来源:origin: stackoverflow.com

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

代码示例来源:origin: alibaba/druid

@Override
public void warn(String s, Throwable e) {
  log.logp(Level.WARNING, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
  warnCount++;
}

代码示例来源:origin: neo4j/neo4j

public static boolean stackTraceContains( Thread thread, Predicate<StackTraceElement> predicate )
{
  for ( StackTraceElement element : thread.getStackTrace() )
  {
    if ( predicate.test( element ) )
    {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: alibaba/druid

@Override
public void info(String msg) {
  log.logp(Level.INFO, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), msg);
  infoCount++;
}

代码示例来源:origin: alibaba/druid

public void warn(String s) {
  log.logp(Level.WARNING, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
  warnCount++;
}

代码示例来源:origin: alibaba/druid

public void error(String s, Throwable e) {
  log.logp(Level.SEVERE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
  errorCount++;
}

代码示例来源:origin: alibaba/druid

public void debug(String s) {
  debugCount++;
  log.logp(Level.FINE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
}

代码示例来源:origin: alibaba/druid

public void debug(String s, Throwable e) {
  debugCount++;
  log.logp(Level.FINE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s, e);
}

代码示例来源:origin: alibaba/druid

public void error(String s) {
  log.logp(Level.SEVERE, loggerName, Thread.currentThread().getStackTrace()[1].getMethodName(), s);
  errorCount++;
}

代码示例来源:origin: androidannotations/androidannotations

protected int getCallerLineNumber() {
    boolean previousWasLogger = false;
    String loggerClassName = Logger.class.getCanonicalName();

    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    for (StackTraceElement stackTraceElement : stackTrace) {
      if (stackTraceElement.getClassName().equals(loggerClassName)) {
        previousWasLogger = true;
      } else if (previousWasLogger) {
        return stackTraceElement.getLineNumber();
      }
    }
    return -1;
  }
}

代码示例来源:origin: apache/flink

public static String getCallLocationName(int depth) {
  StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
  if (stackTrace.length <= depth) {
    return "<unknown>";
  }
  StackTraceElement elem = stackTrace[depth];
  return String.format("%s(%s:%d)", elem.getMethodName(), elem.getFileName(), elem.getLineNumber());
}

代码示例来源:origin: neo4j/neo4j

private void recordOpenCloseMethods()
{
  if ( RECORD_STATEMENTS_TRACES )
  {
    if ( statementOpenCloseCalls.size() > STATEMENT_TRACK_HISTORY_MAX_SIZE )
    {
      statementOpenCloseCalls.pop();
    }
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    statementOpenCloseCalls.add( Arrays.copyOfRange(stackTrace, 2, stackTrace.length) );
  }
}

代码示例来源:origin: apache/rocketmq

public static String currentStackTrace() {
  StringBuilder sb = new StringBuilder();
  StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
  for (StackTraceElement ste : stackTrace) {
    sb.append("\n\t");
    sb.append(ste.toString());
  }
  return sb.toString();
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public void onNext(Integer n) {
    if (depth.get() < MAX_STACK_DEPTH) {
      depth.set(Thread.currentThread().getStackTrace().length);
      a.onNext(n + 1);
    }
  }
});

代码示例来源:origin: neo4j/neo4j

@Override
public LockWaitEvent waitForLock( boolean exclusive, ResourceType resourceType, long... resourceIds )
{
  waitCalls.add( Thread.currentThread().getStackTrace() );
  return this;
}

代码示例来源:origin: apache/zookeeper

static void verifyThreadTerminated(Thread thread, long millis)
  throws InterruptedException
{
  thread.join(millis);
  if (thread.isAlive()) {
    LOG.error("Thread " + thread.getName() + " : "
        + Arrays.toString(thread.getStackTrace()));
    Assert.assertFalse("thread " + thread.getName()
        + " still alive after join", true);
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

protected void assertOnIOContext(Context context) {
  Context current = Vertx.currentContext();
  assertNotNull(current);
  assertEquals(context, current);
  for (StackTraceElement elt : Thread.currentThread().getStackTrace()) {
   if (elt.getMethodName().equals("executeFromIO")) {
    return;
   }
  }
  fail("Not from IO");
 }
}

代码示例来源:origin: neo4j/neo4j

private Throwable buildTimeoutException( Thread thread )
{
  StackTraceElement[] stackTrace = thread.getStackTrace();
  TestTimedOutException timedOutException = new TestTimedOutException( timeout, timeUnit );
  timedOutException.setStackTrace( stackTrace );
  return timedOutException;
}

相关文章