/Users/lyon/j4p/src/thread/Util.java

1    package thread; 
2    import java.util.*; 
3     
4    public class Util { 
5        public static void main(String args[]){ 
6            print(getThreads()); 
7        } 
8     
9        public static ThreadGroup getSystemThreadGroup() { 
10           ThreadGroup systemThreadGroup; 
11           ThreadGroup parentThreadGroup; 
12           systemThreadGroup = Thread.currentThread().getThreadGroup(); 
13           while ((parentThreadGroup = systemThreadGroup.getParent()) != null) 
14               systemThreadGroup = parentThreadGroup; 
15           return systemThreadGroup; 
16       } 
17    
18       /** 
19        * List all the threads that are known and active in the system 
20        * @return  the list of all threads in all groups. 
21        */ 
22       public static Thread[] getThreads() { 
23           ThreadGroup tg[] = getThreadGroups(); 
24           Vector v = new Vector(); 
25           for (int i=0; i < tg.length; i++){ 
26               Thread t[]= getThreads(tg[i]); 
27               for (int j=0; j < t.length; j++ ){ 
28                   if (t[j]!=null) 
29                   v.addElement(t[j]); 
30               } 
31           } 
32           Thread ta[] = new Thread[v.size()]; 
33           v.copyInto(ta); 
34           return ta; 
35    
36       } 
37    
38       private static Thread[] getThreads(ThreadGroup stg) { 
39           Thread ta[] = new Thread[stg.activeCount()]; 
40           stg.enumerate(ta, true); 
41           return ta; 
42       } 
43    
44       public static ThreadGroup[] getThreadGroups() { 
45           ThreadGroup stg = getSystemThreadGroup(); 
46           int nog = stg.activeGroupCount() + 1; 
47           ThreadGroup tga[] = new ThreadGroup[nog]; 
48           stg.enumerate(tga); 
49           tga[tga.length - 1] = stg; 
50           return tga; 
51       } 
52    
53       public static void setName(String name, ThreadGroup tg) { 
54           setName(name, getThreads(tg)); 
55       } 
56       public static void setName(String name, Thread ta[]) { 
57           for (int i=0; i < ta.length; i++) 
58               ta[i].setName(name); 
59       } 
60       public static void print(ThreadGroup tg) { 
61           print(getThreads(tg)); 
62       } 
63    
64       public static void print(Thread o[]) { 
65           for (int i = 0; i < o.length; i++) { 
66               System.out.println(o[i] + 
67                       " isAlive=" + 
68                       o[i].isAlive() + 
69                       " name=" + 
70                       o[i].getName() + 
71                       " isDaemon=" + 
72                       o[i].isDaemon() + 
73                       " priority=" + 
74                       o[i].getPriority()); 
75           } 
76       } 
77    
78    
79       public static void setPriority(Thread ta[], int p) { 
80           for (int i = 0; i < ta.length; i++) 
81               ta[i].setPriority(p); 
82       } 
83    
84       public static void setName(Thread ta[], String n) { 
85           for (int i = 0; i < ta.length; i++) 
86               ta[i].setName(n); 
87       } 
88    
89       public static void setDaemon(Thread ta[], boolean on) { 
90           for (int i = 0; i < ta.length; i++) 
91               setDaemon(ta[i], on); 
92       } 
93    
94       private static void setDaemon(Thread t, boolean on) { 
95           if (t.isAlive() == true) { 
96               System.out.println( 
97                       "The selected threads are in alive" 
98                       + "state ; so you can't change the daemon"); 
99               return; 
100          } 
101          t.setDaemon(on); 
102      } 
103  } 
104   
105