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  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  }