KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > base > Semaphore


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.base;
17
18 /**
19  * A Semaphore class to be used by multiple threads that needs to be notified when a
20  * thread has released this semaphore. Depending threads uses one of the
21  * waitUntilReleased() method to wait for the semaphore, then when the Thread that
22  * owns the Semaphore (called the hold() method) calls the release method all other
23  * threads are awaken.
24  *
25  * All threads that invoke waitUntilReleased() waits until a thread invokes the
26  * release()
27  *
28  * @author erma
29  */

30 public final class Semaphore {
31
32     private Object JavaDoc lockObject = new Object JavaDoc();
33
34     private boolean isReleased = false;
35
36     /**
37      * Creates a new instance of Semaphore
38      */

39     public Semaphore() {
40         this(true);
41     }
42
43     /**
44      * Creates a new instance of Semaphore
45      *
46      * @param isHolding if the semaphore should hold all threads from the begining or
47      * not.
48      */

49     public Semaphore(boolean isHolding) {
50         if (isHolding) {
51             hold();
52         } else {
53             release();
54         }
55     }
56
57     /**
58      * Holds this semaphores. Threads that call the waitUntilReleased stops until the
59      * release() method is called.
60      *
61      * @see #release()
62      */

63     public void hold() {
64         isReleased = false;
65     }
66
67     /**
68      * Waits the current thread until another thread invokes the relase() or
69      * continues if another thread already has invoked the release(). This method
70      * behaves exactly as if it simply performs the call waitUntilReleased(0).
71      *
72      * @throws InterruptedException if another thread has interrupted the current
73      * thread.
74      */

75     public void waitUntilReleased() throws InterruptedException JavaDoc {
76         waitUntilReleased(0);
77     }
78
79     /**
80      * Waits the current thread until another thread invokes the relase() or
81      * continues if another thread already has invoked the release().
82      *
83      * @param timeout the maximum time to wait in milliseconds.
84      * @throws InterruptedException if another thread has interrupted the current
85      * thread.
86      */

87     public void waitUntilReleased(long timeout) throws InterruptedException JavaDoc {
88         if (!isReleased) {
89             synchronized (lockObject) {
90                 lockObject.wait(timeout);
91             }
92         }
93     }
94
95     /**
96      * Release all threads that has invoked the waitUntilReleased().
97      */

98     public void release() {
99         isReleased = true;
100         synchronized (lockObject) {
101             lockObject.notifyAll();
102         }
103     }
104
105     /**
106      * Returns if the semaphore holds all threads that invoke the waitUntilReleased()
107      * methods.
108      *
109      * @return true or false
110      */

111     public boolean isHolding() {
112         return !isReleased;
113     }
114 }
115
Popular Tags