KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > metadata > CountLatch


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.j2ee.metadata;
21
22 /**
23  * A synchronization aid that allows one or more threads to wait until
24  * a set of operations being performed in other threads completes.
25  *
26  * <p>A <tt>CountLatch</tt> is initialized with <em>count</em> 0 (zero).
27  * The {@link #await await} methods block until the current
28  * {@link #getCount count} reaches zero due to invocations of the
29  * {@link #countDown} method, after which all waiting threads are
30  * released and any subsequent invocations of {@link #await await} return
31  * immediately. Count can be increased by invoking {@link #countUp} method.
32  *
33  * @author Martin Adamek
34  */

35 public final class CountLatch {
36     
37     private int count = 0;
38
39     public CountLatch() {
40     }
41     
42     /**
43      * Causes the current thread to wait until the latch has counted down to
44      * zero, unless the thread is {@link Thread#interrupt interrupted}.
45      *
46      * <p>If the current {@link #getCount count} is zero then this method
47      * returns immediately.
48      *
49      * @throws java.lang.InterruptedException
50      */

51     public void await() throws InterruptedException JavaDoc {
52         synchronized (this) {
53             while (count > 0) {
54                 wait();
55             }
56         }
57     }
58     
59     /**
60      * Increments the count of the latch.
61      */

62     public void countUp() {
63         synchronized (this) {
64             count++;
65         }
66     }
67     
68     /**
69      * Decrements the count of the latch, releasing all waiting threads if
70      * the count reaches zero.
71      * <p>If the current {@link #getCount count} is greater than zero then
72      * it is decremented. If the new count is zero then all waiting threads
73      * are re-enabled for thread scheduling purposes.
74      * <p>If the current {@link #getCount count} equals zero then nothing
75      * happens.
76      */

77     public void countDown() {
78         synchronized (this) {
79             if (count != 0) {
80                 count--;
81                 if (count == 0) {
82                     notifyAll();
83                 }
84             }
85         }
86     }
87     
88     /**
89      * Returns the current count.
90      *
91      * @return the current count.
92      */

93     public int getCount() {
94         synchronized (this) {
95             return count;
96         }
97     }
98     
99 }
100
Popular Tags