KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ExecutorGroupBar


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss;
21
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 /**
28  * Allows to synchronize tasks (performed in multiple
29  * ClientRuntime threads) in ExecutorSupport group.
30  *
31  * <p>Example usage:
32  * <pre>
33  * ExecutorGroup group = ...;
34  * group.addExecutor(...);
35  * group.addExecutor(...);
36  * group.addExecutor(...);
37  *
38  * // once executed waits until above executors finish
39  * group.addBarrier(Runnable action);
40  *
41  * // then continue
42  * group.addExecutor(...);
43  * group.addExecutor(...);
44  *
45  * // dispatch into execution queues
46  * group.execute();
47  * </pre>
48  *
49  * @author Petr Kuzel
50  */

51 final class ExecutorGroupBar implements ExecutorGroup.Groupable {
52
53     private final Runnable JavaDoc action;
54     private final ExecutorSupport[] bar;
55     private ExecutorGroup group;
56
57     /**
58      * Creates barrier, with optional action
59      * @param executorsBar collection of ExecutorSupports to wait for.
60      * Actually all other Groupables are relaxed, silently ignored.
61      */

62     public ExecutorGroupBar(Collection JavaDoc executorsBar, Runnable JavaDoc action) {
63         this.action = action;
64
65         // ExecutorSupport.wait(...); works only for ExecutorSupports
66
List JavaDoc filtered = new ArrayList JavaDoc(executorsBar.size());
67         Iterator JavaDoc it = executorsBar.iterator();
68         while (it.hasNext()) {
69             ExecutorGroup.Groupable groupable = (ExecutorGroup.Groupable) it.next();
70             if (groupable instanceof ExecutorSupport) {
71                 filtered.add(groupable);
72             }
73         }
74
75         bar = (ExecutorSupport[]) filtered.toArray(new ExecutorSupport[filtered.size()]);
76     }
77
78     public void joinGroup(ExecutorGroup group) {
79         this.group = group;
80     }
81
82     /**
83      * This one is blocking. It returns when all bar
84      * executors and action finish (successfuly or fail).
85      */

86     public void execute() {
87         group.enqueued(null, this);
88         ExecutorSupport.wait(bar);
89         if (action != null) {
90             action.run();
91         }
92         group.finished(null, this);
93     }
94 }
95
Popular Tags