KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > structural > ParallelJob


1 package org.oddjob.jobs.structural;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 import org.oddjob.Stoppable;
7 import org.oddjob.framework.StructuralJob;
8
9 /**
10  * @oddjob.description
11  *
12  * A job which contains many child jobs and executes
13  * them all in parallel each on a separate thread.
14  * <p>
15  * The return state for this job depends on the return
16  * states of all the children - Complete if all the
17  * children complete, exception if there are any exceptions
18  * in the chilren,
19  * or not complete if any of the children fail to complete.
20  *
21  * @oddjob.example
22  *
23  * <pre>
24  * &lt;parallel&gt;
25  * &lt;echo message="This runs in parallel,"&gt;/>
26  * &lt;echo message="with this."&gt;/>
27  * &lt;/parallel&gt;
28  * </pre>
29  *
30  * @author Rob Gordon
31  */

32
33 public class ParallelJob extends StructuralJob
34             implements Stoppable {
35
36     /**
37      * Called by framework after parsing.
38      *
39      */

40     public void init() {
41         childHelper.initialise();
42     }
43         
44     /**
45      * Add a child job.
46      *
47      * @oddjob.element <i>Any</i>
48      * @oddjob.description The child jobs.
49      * @oddjob.required No, but pointless if missing.
50      *
51      * @param child A child
52      */

53     public void addComponent(Object JavaDoc child) {
54         logger().debug("Adding child [" + child + "]");
55         childHelper.addChild(child);
56     }
57
58     /*
59      * (non-Javadoc)
60      * @see org.oddjob.jobs.AbstractJob#execute()
61      */

62     protected void execute() throws InterruptedException JavaDoc {
63         Vector JavaDoc jobThreads = new Vector JavaDoc();
64         Object JavaDoc[] children = childHelper.getChildren();
65
66         for (int i = 0; i < children.length; ++i) {
67             if (children[i] instanceof Runnable JavaDoc) {
68                 Thread JavaDoc t = new Thread JavaDoc((Runnable JavaDoc) children[i]);
69                 jobThreads.add(t);
70                 t.start();
71             }
72         }
73         
74         for (Enumeration JavaDoc elements = jobThreads.elements();
75                 elements.hasMoreElements() ;) {
76             Thread JavaDoc t = (Thread JavaDoc)elements.nextElement();
77             t.join();
78         }
79     }
80 }
81
Popular Tags