Listing all threads and threadgroups in the VM : ThreadGroup « Thread « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Thread » ThreadGroup 
10. 6. 4. Listing all threads and threadgroups in the VM
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

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(500400);
    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);
  }
}
10. 6. ThreadGroup
10. 6. 1. Demonstrate thread groups.
10. 6. 2. Add thread to thread group
10. 6. 3. Get parent thread group
10. 6. 4. Listing all threads and threadgroups in the VM
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.