Liệt kê Thread, ThreadGroup trong JVM


Mã chương trình sau chỉ ra làm thế nào để liệt kê tất cả các Thread và ThreadGroup đang có mặt trong Java Virtual Machine public class ThreadLister { private static void printThreadInfo(Thread t, String indent) { if (t == null) return; System.out.println…

Mã chương trình sau chỉ ra làm thế nào để liệt kê tất cả các Thread và ThreadGroup đang có mặt trong Java Virtual Machine

  1. public class ThreadLister {
  2. private static void printThreadInfo(Thread t, String indent) {
  3. if (t == null)
  4. return;
  5. System.out.println(indent + “Thread: “+t.getId()+” – “ + t.getName() + ”  Priority: “
  6. + t.getPriority() + (t.isDaemon() ? ” Daemon” : “”)
  7. + (t.isAlive() ? “” : ” Not Alive”));
  8. }
  9. /** Display info about a thread group */
  10. private static void printGroupInfo(ThreadGroup g, String indent) {
  11. if (g == null)
  12. return;
  13. int numThreads = g.activeCount();
  14. int numGroups = g.activeGroupCount();
  15. Thread[] threads = new Thread[numThreads];
  16. ThreadGroup[] groups = new ThreadGroup[numGroups];
  17. g.enumerate(threads, false);
  18. g.enumerate(groups, false);
  19. System.out.println(indent + “Thread Group: “ + g.getName()
  20. ”  Max Priority: “ + g.getMaxPriority()
  21. + (g.isDaemon() ? ” Daemon” : “”));
  22. for (int i = 0; i < numThreads; i++)
  23. printThreadInfo(threads<img src=http://hp-aptech.edu.vn/emoticons/emotion-55.gif&#8221; alt=“Idea” />, indent + ”    “);
  24. for (int i = 0; i < numGroups; i++)
  25. printGroupInfo(groups<img src=http://hp-aptech.edu.vn/emoticons/emotion-55.gif&#8221; alt=“Idea” />, indent + ”    “);
  26. }
  27. /** Find the root thread group and list it recursively */
  28. public static void listAllThreads() {
  29. ThreadGroup currentThreadGroup;
  30. ThreadGroup rootThreadGroup;
  31. ThreadGroup parent;
  32. // Get the current thread group
  33. currentThreadGroup = Thread.currentThread().getThreadGroup();
  34. // Now go find the root thread group
  35. rootThreadGroup = currentThreadGroup;
  36. parent = rootThreadGroup.getParent();
  37. while (parent != null) {
  38. rootThreadGroup = parent;
  39. parent = parent.getParent();
  40. }
  41. printGroupInfo(rootThreadGroup, “”);
  42. }
  43. public static void main(String[] args) {
  44. ThreadLister.listAllThreads();
  45. }
  46. }

public class ThreadLister {   private static void printThreadInfo(Thread t, String indent) {     if (t == null)       return;     System.out.println(indent + “Thread: “+t.getId()+” – ” + t.getName() + ”  Priority: ”         + t.getPriority() + (t.isDaemon() ? ” Daemon” : “”)         + (t.isAlive() ? “” : ” Not Alive”));   }   /** Display info about a thread group */   private static void printGroupInfo(ThreadGroup g, String indent) {     if (g == null)       return;     int numThreads = g.activeCount();     int numGroups = g.activeGroupCount();     Thread[] threads = new Thread[numThreads];     ThreadGroup[] groups = new ThreadGroup[numGroups];     g.enumerate(threads, false);     g.enumerate(groups, false);     System.out.println(indent + “Thread Group: ” + g.getName()         + ”  Max Priority: ” + g.getMaxPriority()         + (g.isDaemon() ? ” Daemon” : “”));     for (int i = 0; i < numThreads; i++)       printThreadInfo(threads<img src=”http://hp-aptech.edu.vn/emoticons/emotion-55.gif&#8221; alt=”Idea” />, indent + ”    “);     for (int i = 0; i < numGroups; i++)       printGroupInfo(groups<img src=”http://hp-aptech.edu.vn/emoticons/emotion-55.gif&#8221; alt=”Idea” />, indent + ”    “);   }   /** Find the root thread group and list it recursively */   public static void listAllThreads() {     ThreadGroup currentThreadGroup;     ThreadGroup rootThreadGroup;     ThreadGroup parent;     // Get the current thread group     currentThreadGroup = Thread.currentThread().getThreadGroup();     // Now go find the root thread group     rootThreadGroup = currentThreadGroup;     parent = rootThreadGroup.getParent();     while (parent != null) {       rootThreadGroup = parent;       parent = parent.getParent();     }     printGroupInfo(rootThreadGroup, “”);   }   public static void main(String[] args) {     ThreadLister.listAllThreads();   } }

Kết quả trả chạy chương trình trên đưa ra những thông tin ví dụ như sau:

Thread Group: system  Max Priority: 10
Thread: 2 – Reference Handler  Priority: 10 Daemon
Thread: 3 – Finalizer  Priority: 8 Daemon
Thread: 4 – Signal Dispatcher  Priority: 9 Daemon
Thread Group: main  Max Priority: 10
Thread: 1 – main  Priority: 5

[HaiPhong-Aptech]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s