View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.threads;
18  
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /***
27   * Represents a group of threads. This is not the same as Java's thread groups.
28   * All of the threads in a thread group are started at the same time, not as they
29   * are defined. Use this in conjunction with other tags like join to manipulate
30   * a group of threads.
31   *
32   * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
33   */
34  
35  public class GroupTag extends TagSupport {
36      /*** Variable to place the thread group into */
37      private String var = null;
38      /*** The thread list */
39      private List threads = new ArrayList();
40  
41      /*** Child threads will add themselves and will then all be started together */
42      public void doTag(XMLOutput output) throws JellyTagException {
43          invokeBody(output);
44  
45          // store the group in a jelly variable
46          if (var != null) {
47              context.setVariable(var, threads);
48          }
49  
50          // start the threads
51          for (int i = 0; i < threads.size(); i++) {
52              Thread thread = (Thread) threads.get(i);
53              thread.start();
54          }
55      }
56  
57      /*** Add a thread to the thread group list */
58      public void addThread(Thread thread) {
59          threads.add(thread);
60      }
61  
62      /*** Get the list of threads in this thread group */
63      public List getThreads() {
64          return threads;
65      }
66  
67      /*** Set the variable name to store the thread group in */
68      public void setVar(String var) {
69          this.var = var;
70      }
71  }