中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

列出JVM中所有的線程組和線程

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
這個(gè)類(lèi)包含一個(gè)有用的靜態(tài)方法列出所有在JVM中的線程和線程組。它也有一個(gè)簡(jiǎn)單的main()方法,以便它可以作為一個(gè)獨(dú)立的程序運(yùn)行。
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);
  }
}

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP算式驗(yàn)證碼和漢字驗(yàn)證碼的實(shí)現(xiàn)方法

下一篇:根據(jù)URL抓取并生成縮略圖的Java代碼