加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

列出JVM中所有的线程组和线程

发布时间:2020-12-14 23:52:30 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 这个类包含一个有用的静态方法列出所有在JVM中的线程和线程组。它也有一个简单的main()方法,以便它可以作为一个独立的程序运行。 import java.awt.

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

这个类包含一个有用的静态方法列出所有在JVM中的线程和线程组。它也有一个简单的main()方法,以便它可以作为一个独立的程序运行。
import java.awt.BorderLayout;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * This class contains a useful static method for listing all threads and
 * threadgroups in the VM. It also has a simple main() method so it can be run
 * as a standalone program.
 */
public class ThreadLister {
  /** Display information about a thread. */
  private static void printThreadInfo(PrintWriter out,Thread t,String indent) {
    if (t == null)
      return;
    out.println(indent + "Thread: " + t.getName() + "  Priority: " + t.getPriority()
        + (t.isDaemon() ? " Daemon" : "") + (t.isAlive() ? "" : " Not Alive"));
  }

  /** Display info about a thread group and its threads and groups */
  private static void printGroupInfo(PrintWriter out,ThreadGroup g,String indent) {
    if (g == null)
      return;
    int num_threads = g.activeCount();
    int num_groups = g.activeGroupCount();
    Thread[] threads = new Thread[num_threads];
    ThreadGroup[] groups = new ThreadGroup[num_groups];

    g.enumerate(threads,false);
    g.enumerate(groups,false);

    out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
        + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < num_threads; i++)
      printThreadInfo(out,threads[i],indent + "    ");
    for (int i = 0; i < num_groups; i++)
      printGroupInfo(out,groups[i],indent + "    ");
  }

  /** Find the root thread group and list it recursively */
  public static void listAllThreads(PrintWriter out) {
    ThreadGroup current_thread_group;
    ThreadGroup root_thread_group;
    ThreadGroup parent;

    // Get the current thread group
    current_thread_group = Thread.currentThread().getThreadGroup();

    // Now go find the root thread group
    root_thread_group = current_thread_group;
    parent = root_thread_group.getParent();
    while (parent != null) {
      root_thread_group = parent;
      parent = parent.getParent();
    }

    // And list it,recursively
    printGroupInfo(out,root_thread_group,"");
  }

  /**
   * The main() method create a simple graphical user interface to display the
   * threads in. This allows us to see the "event dispatch thread" used by AWT
   * and Swing.
   */
  public static void main(String[] args) {
    // Create a simple Swing GUI
    JFrame frame = new JFrame("ThreadLister Demo");
    JTextArea textarea = new JTextArea();
    frame.getContentPane().add(new JScrollPane(textarea),BorderLayout.CENTER);
    frame.setSize(500,400);
    frame.setVisible(true);

    // Get the threadlisting as a string using a StringWriter stream
    StringWriter sout = new StringWriter(); // To capture the listing
    PrintWriter out = new PrintWriter(sout);
    ThreadLister.listAllThreads(out); // List threads to stream
    out.close();
    String threadListing = sout.toString(); // Get listing as a string

    // Finally,display the thread listing in the GUI
    textarea.setText(threadListing);
  }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读