Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: marf/util/ExpandedThreadGroup.java


1   /*
2    * Created on Apr 20, 2004
3    */
4   package marf.util;
5   
6   import java.util.Vector;
7   
8   
9   /**
10   * <p>Provides some useful extensions to java.lang.ThreadGroup one would
11   * normally expect to be in a "group".</p>
12   * 
13   * <p>Maintains local references to the group-belonging threads
14   * for extra group control in a form of a Vector.</p>
15   *
16   * $Id: ExpandedThreadGroup.java,v 1.10 2005/06/20 05:34:32 mokhov Exp $
17   *
18   * @author Serguei Mokhov
19   * @version $Revision: 1.10 $
20   * @since 0.3.0
21   */
22  public class ExpandedThreadGroup
23  extends ThreadGroup
24  {
25    /**
26     * Local refernces to the threads belonging to this group.
27     */
28    protected Vector oGroup = new Vector(); 
29    
30    /**
31     * ThreadGroup and name constructor inherited from the parent.
32     * @param poParent Parent group
33     * @param pstrName Group's name
34     */
35    public ExpandedThreadGroup(ThreadGroup poParent, String pstrName)
36    {
37      super(poParent, pstrName);
38    }
39  
40    /**
41     * Mimics parent's constructor.
42     * @param pstrName Group's name
43     */
44    public ExpandedThreadGroup(String pstrName)
45    { 
46      super(pstrName);
47    }
48      
49    /**
50     * Mimics parent's constructor.
51     * @param poParent Parent group
52     * @param pstrName Group's name
53     */
54    public ExpandedThreadGroup(ExpandedThreadGroup poParent, String pstrName)
55    {  
56      super(poParent, pstrName);
57    }
58  
59    /**
60     * Starts all non-started threads in this group.
61     */
62    public void start()
63    {
64      Thread[] aoBabies = enumerate(false); 
65        
66      for(int i = 0; i < aoBabies.length; i++)
67      {
68        /*
69         * Since starting threads is not totally under our
70         * control, we start only those that have not been
71         * started yet.
72         */
73        if(aoBabies[i].isAlive() == false)
74          aoBabies[i].start();
75      }
76    }
77    
78    /**
79     * Wait for all the threads in the group to terminate.
80     * @throws InterruptedException if one of the threads is
81     * interruped or a thread group has been started
82     */
83    public void join()
84    throws InterruptedException
85    {
86      try
87      {    
88        Thread[] aoBabies = enumerate(true); 
89          
90        for(int i = 0; i < aoBabies.length; i++)
91          aoBabies[i].join();
92      }
93      catch(NullPointerException e)
94      {
95        throw new InterruptedException
96        (
97          "Threads (a thread group) have to be started first, " + 
98          "before calling join() onto them."
99        );
100     }
101   }
102 
103   /**
104    * Provides an enumeration that allocates a new array and populates
105    * it with currently active threads with ThreadGroup's enumerate().
106    *
107    * @param pbActiveThreads if only active threads needed, false if all
108    * @return array of threads belonging to this group
109    */
110   public Thread[] enumerate(boolean pbActiveThreads)
111   {
112     Thread[] aoBabies = new Thread[pbActiveThreads ? activeCount() : this.oGroup.size()];
113 
114     if(pbActiveThreads == true) 
115       enumerate(aoBabies);
116     else
117       this.oGroup.copyInto(aoBabies);
118 
119     return aoBabies;
120   }
121 
122   /**
123    * Adds specified thread to the local reference list.
124    * @param poThread thread object to add
125    */
126   public void addThread(Thread poThread)
127   {
128     this.oGroup.add(poThread);
129   }
130 
131   /**
132    * Retrieves class' revision.
133    * @return revision string
134    */
135   public static String getMARFSourceCodeRevision()
136   {
137     return "$Revision: 1.10 $";
138   }
139 }
140 
141 // EOF