01: /*
02: * BEGIN_HEADER - DO NOT EDIT
03: *
04: * The contents of this file are subject to the terms
05: * of the Common Development and Distribution License
06: * (the "License"). You may not use this file except
07: * in compliance with the License.
08: *
09: * You can obtain a copy of the license at
10: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
11: * See the License for the specific language governing
12: * permissions and limitations under the License.
13: *
14: * When distributing Covered Code, include this CDDL
15: * HEADER in each file and include the License file at
16: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
17: * If applicable add the following below this CDDL HEADER,
18: * with the fields enclosed by brackets "[]" replaced with
19: * your own identifying information: Portions Copyright
20: * [year] [name of copyright owner]
21: */
22:
23: /*
24: * @(#)OperationCounter.java
25: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
26: *
27: * END_HEADER - DO NOT EDIT
28: */
29: package com.sun.jbi.framework;
30:
31: /**
32: * This class provides a counter that is used with a set of threaded operations.
33: * It provides methods for incrementing and decrementing the counter. This
34: * class is used when a thread is creating a number of instances of a class
35: * extending the <CODE>Operation</CODE> abstract class to run on separate
36: * threads, optionally under a time limit. The creating thread creates an
37: * instance of this class, and passes that instance to the constructor for the
38: * <CODE>Operation</CODE> subclass instances as it creates them. The <CODE>
39: * Operation</CODE> constructor calls the <CODE>incrememt</CODE> method on this
40: * class, automatically keeping a count of the number of threaded operations
41: * that have been created. All of the operations are then started on separate
42: * threads, and the creating thread calls the <CODE>wait</CODE> method on the
43: * instance of this class, optionally with the desired timeout value. As the
44: * <CODE>Operation</CODE> instances complete their processing, their <CODE>
45: * run</CODE> methods call the <CODE>decrement</CODE> method on the instance
46: * of this class. When the last operation completes, the <CODE>decrement</CODE>
47: * method sets the counter to zero, and calls <CODE>notify</CODE> to end the
48: * wait on the original thread. If the wait times out, the <CODE>Operation
49: * </CODE> instances can be checked to determine which one(s) did not complete.
50: *
51: * @author Sun Microsystems, Inc.
52: */
53: class OperationCounter {
54: /**
55: * Counter of outstanding operations.
56: */
57: private int mCounter;
58:
59: /**
60: * Increment the counter.
61: *
62: * @return The new value of the counter.
63: */
64: synchronized int increment() {
65: return ++mCounter;
66: }
67:
68: /**
69: * Decrement the counter. If it reaches zero, notify all waiting threads.
70: *
71: * @return The new value of the counter.
72: */
73: synchronized int decrement() {
74: if (--mCounter == 0) {
75: notifyAll();
76: }
77: return mCounter;
78: }
79:
80: /**
81: * Get the the counter value.
82: *
83: * @return The value of the counter.
84: */
85: synchronized int getValue() {
86: return mCounter;
87: }
88:
89: }
|