1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46 if (var != null) {
47 context.setVariable(var, threads);
48 }
49
50
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 }