01: package com.clarkware.junitperf;
02:
03: /**
04: * The <code>ThreadBarrier</code> class provides a callback
05: * method for threads to signal their completion.
06: *
07: * @author <b>Mike Clark</b>
08: * @author Clarkware Consulting, Inc.
09: */
10:
11: public class ThreadBarrier {
12:
13: public int returnedCount;
14: public final int dispatchedCount;
15:
16: /**
17: * Constructs a <code>ThreadBarrier</code> with the
18: * specified number of threads to wait for.
19: *
20: * @param numDispatched Number of threads dispatched.
21: */
22: public ThreadBarrier(int numDispatched) {
23: returnedCount = 0;
24: dispatchedCount = numDispatched;
25: }
26:
27: /**
28: * Called when the specified thread is complete.
29: *
30: * @param t Completed thread.
31: */
32: public synchronized void onCompletion(Thread t) {
33: returnedCount++;
34: }
35:
36: /**
37: * Determines whether the thread barrier has been reached -
38: * when all dispatched threads have returned.
39: *
40: * @return <code>true</code> if the barrier has been reached;
41: * <code>false</code> otherwise.
42: */
43: public boolean isReached() {
44: return (returnedCount >= dispatchedCount);
45: }
46:
47: /**
48: * Cancels the specified number of threads.
49: *
50: * @param threadCount Number of threads to cancel.
51: */
52: public synchronized void cancelThreads(int threadCount) {
53: returnedCount += threadCount;
54: }
55: }
|