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

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

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

Thread.getAllStackTraces介绍

[英]Returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread. The returned stack traces are in the format specified for the #getStackTrace method.

The threads may be executing while this method is called. The stack trace of each thread only represents a snapshot and each stack trace may be obtained at different time. A zero-length array will be returned in the map value if the virtual machine has no stack trace information about a thread.

If there is a security manager, then the security manager's checkPermission method is called with a RuntimePermission("getStackTrace") permission as well as RuntimePermission("modifyThreadGroup") permission to see if it is ok to get the stack trace of all threads.
[中]返回所有活动线程的堆栈跟踪映射。映射键是线程,每个映射值是一个StackTraceElement数组,表示对应线程的堆栈转储。返回的堆栈跟踪采用为#getStackTrace方法指定的格式。
调用此方法时,线程可能正在执行。每个线程的堆栈跟踪仅代表一个快照,每个堆栈跟踪可能在不同的时间获得。如果虚拟机没有关于线程的堆栈跟踪信息,则将在映射值中返回零长度数组。
如果存在安全管理器,则会使用RuntimePermission(“getStackTrace”)权限和RuntimePermission(“modifyThreadGroup”)权限调用安全管理器的checkPermission方法,以查看是否可以获取所有线程的堆栈跟踪。

代码示例

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

Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

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

/** {@inheritDoc} */
  @Override public boolean stopThreadById(long id) {
    Thread[] threads = Thread.getAllStackTraces().keySet().stream()
      .filter(t -> t.getId() == id)
      .toArray(Thread[]::new);

    if (threads.length != 1)
      return false;

    threads[0].stop();

    return true;
  }
}

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

public static String jstack() {
  return jstack(Thread.getAllStackTraces());
}

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

/**
 * Finds all threads and corresponding stack traces which can potentially cause the
 * {@link ExecutorService executor} to not terminate in {@link #AWAIT_TIMEOUT_MINUTES} minutes.
 *
 * @return thread dump as string.
 */
private static String allStackTraces()
{
  return Thread.getAllStackTraces()
      .entrySet()
      .stream()
      .map( entry -> Exceptions.stringify( entry.getKey(), entry.getValue() ) )
      .collect( joining() );
}

代码示例来源:origin: jenkinsci/jenkins

public static Map<Thread,StackTraceElement[]> dumpAllThreads() {
  Map<Thread,StackTraceElement[]> sorted = new TreeMap<Thread,StackTraceElement[]>(new ThreadSorter());
  sorted.putAll(Thread.getAllStackTraces());
  return sorted;
}

代码示例来源:origin: Alluxio/alluxio

/**
 * Logs a stack trace for all threads currently running in the JVM, similar to jstack.
 */
public static void logAllThreads() {
 StringBuilder sb = new StringBuilder("Dumping all threads:\n");
 for (Thread t : Thread.getAllStackTraces().keySet()) {
  sb.append(formatStackTrace(t));
 }
 LOG.info(sb.toString());
}

代码示例来源:origin: vipshop/vjtools

/**
 * 强行打印ThreadDump,使用最轻量的采集方式,不打印锁信息
 */
public void threadDump(String reasonMsg) {
  logger.info("Thread dump by ThreadDumpper" + (reasonMsg != null ? (" for " + reasonMsg) : ""));
  Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
  // 两条日志间的时间间隔,是VM被thread dump堵塞的时间.
  logger.info("Finish the threads snapshot");
  StringBuilder sb = new StringBuilder(8192 * 20).append('\n');
  for (Entry<Thread, StackTraceElement[]> entry : threads.entrySet()) {
    dumpThreadInfo(entry.getKey(), entry.getValue(), sb);
  }
  logger.info(sb.toString());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Override
  public Map<ThreadInformation, StackTraceElement[]> createThreadInfo() {
    final Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
    final Map<ThreadInformation, StackTraceElement[]> threads =
      new HashMap<>(map.size());
    for (final Map.Entry<Thread, StackTraceElement[]> entry : map.entrySet()) {
      threads.put(new BasicThreadInformation(entry.getKey()), entry.getValue());
    }
    return threads;
  }
}

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

private static String getAllThreadStacksAsString() {
 Map<Thread, StackTraceElement[]> threadStacks = Thread.getAllStackTraces();
 StringBuilder sb = new StringBuilder();
 for (Map.Entry<Thread, StackTraceElement[]> entry : threadStacks.entrySet()) {
  Thread t = entry.getKey();
  sb.append(System.lineSeparator());
  sb.append("Name: ").append(t.getName()).append(" State: ").append(t.getState());
  addStackString(entry.getValue(), sb);
 }
 return sb.toString();
}

代码示例来源:origin: Netflix/eureka

private boolean containsJerseyThread() {
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    for (Thread t : threads) {
      if (t.getName().contains(THREAD_NAME)) {
        return true;
      }
    }
    return false;
  }
}

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

@After
public void after() {
  try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getName().startsWith("RxNewThread")) {
      --count;
    }
  }
  if (count != 0) {
    throw new IllegalStateException("NewThread leak!");
  }
}

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

@After
public void after() {
  try {
    Thread.sleep(100);
  } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getName().startsWith("RxNewThread")) {
      --count;
    }
  }
  if (count != 0) {
    throw new IllegalStateException("NewThread leak!");
  }
}

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

@Before
public void before() {
  stringObserver = TestHelper.mockObserver();
  for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getName().startsWith("RxNewThread")) {
      count++;
    }
  }
}

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

@Before
public void before() {
  stringSubscriber = TestHelper.mockSubscriber();
  for (Thread t : Thread.getAllStackTraces().keySet()) {
    if (t.getName().startsWith("RxNewThread")) {
      count++;
    }
  }
}

代码示例来源:origin: SonarSource/sonarqube

private static Thread getThread(String name) {
  Set<Thread> threads = Thread.getAllStackTraces().keySet();

  for (Thread t : threads) {
   if (t.getName().equals(name)) {
    return t;
   }
  }
  return null;
 }
}

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

public static void logAllStackTraces() {
  StringBuilder sb = new StringBuilder();
  sb.append("Starting logAllStackTraces()\n");
  Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
  for (Entry<Thread, StackTraceElement[]> e: threads.entrySet()) {
    sb.append("Thread " + e.getKey().getName() + "\n");
    for (StackTraceElement elem: e.getValue()) {
      sb.append("\tat " + elem + "\n");
    }
  }
  sb.append("Ending logAllStackTraces()\n");
  LOG.error(sb.toString());
}

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

for (Thread t : Thread.getAllStackTraces().keySet()) {
  if (t.getName().startsWith("Rx")) {
    rxThreadsBefore.add(t);
for (Thread t : Thread.getAllStackTraces().keySet()) {
  if (t.getName().startsWith("Rx")) {
    rxThreadsAfter.add(t);

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

private static String getAllThreadStacksAsString() {
 Map<Thread, StackTraceElement[]> threadStacks = Thread.getAllStackTraces();
 StringBuilder sb = new StringBuilder();
 for (Map.Entry<Thread, StackTraceElement[]> entry : threadStacks.entrySet()) {
  Thread t = entry.getKey();
  sb.append(System.lineSeparator());
  sb.append("Name: ").append(t.getName()).append(" State: ").append(t.getState());
  MetaStoreTestUtils.addStackString(entry.getValue(), sb);
 }
 return sb.toString();
}

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

for (Thread t : Thread.getAllStackTraces().keySet()) {
  if (t.getName().startsWith("Rx")) {
    rxThreads.add(t);

代码示例来源:origin: ben-manes/caffeine

public static void handleTimout(Queue<String> failures, ExecutorService es, TimeoutException e) {
 for (StackTraceElement[] trace : Thread.getAllStackTraces().values()) {
  for (StackTraceElement element : trace) {
   logger.info("\tat " + element);
  }
  if (trace.length > 0) {
   logger.info("------");
  }
 }
 MoreExecutors.shutdownAndAwaitTermination(es, 10, TimeUnit.SECONDS);
 for (String failure : failures) {
  logger.debug(failure);
 }
 fail("Spun forever", e);
}

相关文章