Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ThreadLister.java
1 package ij.plugin;
2 import java.io.*;
3 import ij.*;
4 import ij.text.*;
5 
12 public class ThreadLister implements PlugIn {
13 
14  public void run(String arg) {
15  if (IJ.getApplet()!=null)
16  return;
17  CharArrayWriter caw = new CharArrayWriter();
18  PrintWriter pw = new PrintWriter(caw);
19  try {
20  listAllThreads(pw);
21  new TextWindow("Threads", caw.toString(), 300, 300);
22  } catch
23  (Exception e) {}
24  }
25 
26 
27  // Display info about a thread.
28  private static void print_thread_info(PrintWriter out, Thread t,
29  String indent) {
30  if (t == null) return;
31  out.print(indent + "Thread: " + t.getName() +
32  " Priority: " + t.getPriority() +
33  (t.isDaemon()?" Daemon":"") +
34  (t.isAlive()?"":" Not Alive") + "\n");
35  }
36 
37  // Display info about a thread group and its threads and groups
38  private static void list_group(PrintWriter out, ThreadGroup g,
39  String indent) {
40  if (g == null) return;
41  int num_threads = g.activeCount();
42  int num_groups = g.activeGroupCount();
43  Thread[] threads = new Thread[num_threads];
44  ThreadGroup[] groups = new ThreadGroup[num_groups];
45 
46  g.enumerate(threads, false);
47  g.enumerate(groups, false);
48 
49  out.println(indent + "Thread Group: " + g.getName() +
50  " Max Priority: " + g.getMaxPriority() +
51  (g.isDaemon()?" Daemon":"") + "\n");
52 
53  for(int i = 0; i < num_threads; i++)
54  print_thread_info(out, threads[i], indent + " ");
55  for(int i = 0; i < num_groups; i++)
56  list_group(out, groups[i], indent + " ");
57  }
58 
59  // Find the root thread group and list it recursively
60  public static void listAllThreads(PrintWriter out) {
61  ThreadGroup current_thread_group;
62  ThreadGroup root_thread_group;
63  ThreadGroup parent;
64 
65  // Get the current thread group
66  current_thread_group = Thread.currentThread().getThreadGroup();
67 
68  // Now go find the root thread group
69  root_thread_group = current_thread_group;
70  parent = root_thread_group.getParent();
71  while(parent != null) {
72  root_thread_group = parent;
73  parent = parent.getParent();
74  }
75 
76  // And list it, recursively
77  list_group(out, root_thread_group, "");
78  }
79 
80 }