1 |
tim |
1.1 |
package java.util.concurrent; |
2 |
|
|
|
3 |
|
|
/** |
4 |
|
|
* A <tt>CyclicBarrier</tt> allows a set threads to all wait for each |
5 |
|
|
* other to reach a common barrier point. They are useful in programs |
6 |
|
|
* involving a fixed sized party of threads that must occasionally |
7 |
|
|
* wait for each other. The barrier is <em>cyclic</em> because it can |
8 |
|
|
* be re-used after the waiting threads are released. |
9 |
|
|
* |
10 |
|
|
* <p>A <tt>CyclicBarrier</tt> supports an optional {@link Runnable} command |
11 |
|
|
* that is run once per barrier point, after the last thread in the party |
12 |
|
|
* arrives, but before any threads are released. |
13 |
|
|
* This <em>barrier action</em> is useful |
14 |
|
|
* for updating shared-state before any of the parties continue. |
15 |
|
|
* |
16 |
|
|
* <p><b>Sample usage:</b> Here is a code sketch of |
17 |
|
|
* using a barrier in a parallel decomposition design: |
18 |
|
|
* <pre> |
19 |
|
|
* class Solver { |
20 |
|
|
* final int N; |
21 |
|
|
* final float[][] data; |
22 |
|
|
* final CyclicBarrier barrier; |
23 |
|
|
* |
24 |
|
|
* class Worker implements Runnable { |
25 |
|
|
* int myRow; |
26 |
|
|
* Worker(int row) { myRow = row; } |
27 |
|
|
* public void run() { |
28 |
|
|
* while (!done()) { |
29 |
|
|
* processRow(myRow); |
30 |
|
|
* |
31 |
|
|
* try { |
32 |
|
|
* barrier.await(); |
33 |
|
|
* } |
34 |
|
|
* catch (InterruptedException ex) { return; } |
35 |
|
|
* catch (BrokenBarrierException ex) { return; } |
36 |
|
|
* } |
37 |
|
|
* } |
38 |
|
|
* } |
39 |
|
|
* |
40 |
|
|
* public Solver(float[][] matrix) { |
41 |
|
|
* data = matrix; |
42 |
|
|
* N = matrix.length; |
43 |
|
|
* barrier = new CyclicBarrier(N, |
44 |
|
|
* new Runnable() { |
45 |
|
|
* public void run() { |
46 |
|
|
* mergeRows(...); |
47 |
|
|
* } |
48 |
|
|
* }); |
49 |
|
|
* for (int i = 0; i < N; ++i) |
50 |
|
|
* new Thread(new Worker(i)).start(); |
51 |
|
|
* |
52 |
|
|
* waitUntilDone(); |
53 |
|
|
* } |
54 |
|
|
* } |
55 |
|
|
* </pre> |
56 |
|
|
* Here, each worker thread processes a row of the matrix then waits at the |
57 |
|
|
* barrier until all rows have been processed. When all rows are processed |
58 |
|
|
* the supplied {@link Runnable} barrier action is executed and merges the |
59 |
|
|
* rows. If the merger |
60 |
|
|
* determines that a solution has been found then <tt>done()</tt> will return |
61 |
|
|
* <tt>true</tt> and each worker will terminate. |
62 |
|
|
* |
63 |
|
|
* <p>If the barrier action does not rely on the parties being suspended when |
64 |
|
|
* it is executed, then any of the threads in the party could execute that |
65 |
|
|
* action when it is released. To facilitate this, each invocation of |
66 |
|
|
* {@link #await} returns the arrival index of that thread at the barrier. |
67 |
|
|
* You can then choose which thread should execute the barrier action, for |
68 |
|
|
* example: |
69 |
|
|
* <pre> if (barrier.await() == 0) { |
70 |
|
|
* // log the completion of this iteration |
71 |
|
|
* }</pre> |
72 |
|
|
* |
73 |
|
|
* <p>The <tt>CyclicBarrier</tt> uses an all-or-none breakage model for failed |
74 |
|
|
* synchronization attempts: If a thread leaves a barrier point |
75 |
|
|
* prematurely because of interruption, all others will also |
76 |
|
|
* leave abnormally (via {@link BrokenBarrierException}), until the barrier is |
77 |
|
|
* {@link #reset}. This is usually the simplest and best |
78 |
|
|
* strategy for sharing knowledge about failures among cooperating |
79 |
|
|
* threads in the most common usage contexts of barriers. |
80 |
|
|
* |
81 |
|
|
* <h3>Implementation Considerations</h3> |
82 |
|
|
* <p>This implementation has the property that interruptions among newly |
83 |
|
|
* arriving threads can cause as-yet-unresumed threads from a previous |
84 |
|
|
* barrier cycle to return out as broken. This transmits breakage as |
85 |
|
|
* early as possible, but with the possible byproduct that only some |
86 |
|
|
* threads returning out of a barrier will realize that it is newly |
87 |
|
|
* broken. (Others will not realize this until a future cycle.) |
88 |
|
|
* |
89 |
|
|
* |
90 |
|
|
* |
91 |
|
|
* @since 1.5 |
92 |
|
|
* @spec JSR-166 |
93 |
|
|
* @revised $Date: 2003/04/25 13:27:07 $ |
94 |
|
|
* @editor $Author: dl $ |
95 |
|
|
* |
96 |
|
|
* @fixme Is the above property actually true in this implementation? |
97 |
|
|
* @fixme Should we have a timeout version of await()? |
98 |
|
|
*/ |
99 |
|
|
public class CyclicBarrier { |
100 |
|
|
|
101 |
|
|
/** |
102 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
103 |
|
|
* given number of parties (threads) are waiting upon it, and which |
104 |
|
|
* will execute the given barrier action when the barrier is tripped. |
105 |
|
|
* |
106 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
107 |
|
|
* before the barrier is tripped. |
108 |
|
|
* @param barrierAction the command to execute when the barrier is |
109 |
|
|
* tripped. |
110 |
|
|
* |
111 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
112 |
|
|
*/ |
113 |
|
|
public CyclicBarrier(int parties, Runnable barrierAction) { |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/** |
117 |
|
|
* Create a new <tt>CyclicBarrier</tt> that will trip when the |
118 |
|
|
* given number of parties (threads) are waiting upon it. |
119 |
|
|
* |
120 |
|
|
* <p>This is equivalent to <tt>CyclicBarrier(parties, null)</tt>. |
121 |
|
|
* |
122 |
|
|
* @param parties the number of threads that must invoke {@link #await} |
123 |
|
|
* before the barrier is tripped. |
124 |
|
|
* |
125 |
|
|
* @throws IllegalArgumentException if <tt>parties</tt> is less than 1. |
126 |
|
|
*/ |
127 |
|
|
public CyclicBarrier(int parties) { |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Return the number of parties required to trip this barrier. |
132 |
|
|
* @return the number of parties required to trip this barrier. |
133 |
|
|
**/ |
134 |
|
|
public int getParties() { |
135 |
|
|
return 0; // for now |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
/** |
139 |
|
|
* Wait until all {@link #getParties parties} have invoked <tt>await</tt> |
140 |
|
|
* on this barrier. |
141 |
|
|
* |
142 |
|
|
* <p>If the current thread is not the last to arrive then it is |
143 |
|
|
* disabled for thread scheduling purposes and lies dormant until |
144 |
|
|
* one of four things happens: |
145 |
|
|
* <ul> |
146 |
|
|
* <li>The last thread arrives; or |
147 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} the current |
148 |
|
|
* thread; or |
149 |
|
|
* <li>Some other thread {@link Thread#interrupt interrupts} one of the |
150 |
|
|
* other waiting threads; or |
151 |
|
|
* <li>Some other thread invokes {@link #reset} on this barrier. |
152 |
|
|
* </ul> |
153 |
|
|
* <p>If the current thread: |
154 |
|
|
* <ul> |
155 |
|
|
* <li>has its interrupted status set on entry to this method; or |
156 |
|
|
* <li>is {@link Thread#interrupt interrupted} while waiting |
157 |
|
|
* </ul> |
158 |
|
|
* then {@link InterruptedException} is thrown and the current thread's |
159 |
|
|
* interrupted status is cleared. |
160 |
|
|
* |
161 |
|
|
* <p>If the barrier is {@link #reset} while any thread is waiting, or if |
162 |
|
|
* the barrier {@link #isBroken is broken} when <tt>await</tt> is invoked |
163 |
|
|
* then {@link BrokenBarrierException} is thrown. |
164 |
|
|
* |
165 |
|
|
* <p>If any thread is {@link Thread#interrupt interrupted} while waiting, |
166 |
|
|
* then all other waiting threads will throw |
167 |
|
|
* {@link BrokenBarrierException} and the barrier is placed in the broken |
168 |
|
|
* state. |
169 |
|
|
* |
170 |
|
|
* <p>If the current thread is the last thread to arrive, and a |
171 |
|
|
* non-null barrier action was supplied in the constructor, then the |
172 |
|
|
* current thread runs the action before allowing the other threads to |
173 |
|
|
* continue. |
174 |
|
|
* If an exception occurs during the barrier action then that exception |
175 |
|
|
* will be propagated in the current thread. |
176 |
|
|
* |
177 |
|
|
* @return the arrival index of the current thread, where index |
178 |
|
|
* <tt>{@link #getParties()} - 1</tt> indicates the first to arrive and |
179 |
|
|
* zero indicates the last to arrive. |
180 |
|
|
* |
181 |
|
|
* @throws InterruptedException if the current thread was interrupted |
182 |
|
|
* while waiting |
183 |
|
|
* @throws BrokenBarrierException if <em>another</em> thread was |
184 |
|
|
* interrupted while the current thread was waiting, or the barrier was |
185 |
|
|
* reset, or the barrier was broken when <tt>await</tt> was called. |
186 |
|
|
*/ |
187 |
|
|
public int await() throws InterruptedException, BrokenBarrierException { |
188 |
|
|
return 0; // for now |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* Query if this barrier is in a broken state. |
193 |
|
|
* @return <tt>true</tt> if one or more parties broke out of this |
194 |
|
|
* barrier due to interruption since construction or the last reset; |
195 |
|
|
* and <tt>false</tt> otherwise. |
196 |
|
|
*/ |
197 |
|
|
public boolean isBroken() { |
198 |
|
|
return false; // for now |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
/** |
202 |
|
|
* Reset the barrier to its initial state. If any parties are |
203 |
|
|
* currently waiting at the barrier, they will return with a |
204 |
|
|
* {@link BrokenBarrierException}. |
205 |
|
|
*/ |
206 |
|
|
public void reset() { |
207 |
|
|
// for now |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
/** |
211 |
|
|
* Return the number of parties currently waiting at the barrier. |
212 |
|
|
* This method is primarily useful for debugging and assertions. |
213 |
|
|
* |
214 |
|
|
* @return the number of parties currently blocked in {@link #await} |
215 |
|
|
**/ |
216 |
|
|
public int getNumberWaiting() { |
217 |
|
|
return 0; // for now |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
|