1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.threads;
17
18 import org.apache.commons.jelly.XMLOutput;
19
20 import java.util.List;
21
22 /***
23 * A thread join waits until a thread or threadGroup is complete.
24 *
25 * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
26 */
27
28 public class JoinTag extends UseThreadTag {
29 /*** how long to wait */
30 private long timeout = -1;
31
32 /*** Perform the thread join */
33 protected void useThread(Thread thread, XMLOutput output) throws InterruptedException {
34 joinThread(thread);
35 }
36
37 /*** Join all of the threads in a thread group */
38 protected void useThreadGroup(List threadGroup, XMLOutput output) throws InterruptedException {
39 for (int i = 0; i < threadGroup.size(); i++) {
40 joinThread((Thread) threadGroup.get(i));
41 }
42 }
43
44 /*** Join a thread */
45 private void joinThread(Thread thread) throws InterruptedException {
46 if (timeout > 0) {
47 thread.join(timeout);
48 } else {
49 thread.join();
50 }
51 }
52
53 /***
54 * How long should the join wait. If <= 0 the join waits until the
55 * thread is dead.
56 * @param timeout in millis
57 */
58 public void setTimeout(long timeout) {
59 this.timeout = timeout;
60 }
61 }