KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > thread > Valve


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.thread;
19
20 /**
21  * A Valve is a synchronization object used enable or disable the "flow" of concurrent
22  * processing.
23  *
24  *
25  * @version $Revision: 1.2 $
26  */

27 final public class Valve {
28     
29     private final Object JavaDoc mutex = new Object JavaDoc();
30     private boolean on;
31     private int turningOff=0;
32     private int usage=0;
33
34     public Valve(boolean on) {
35         this.on = on;
36     }
37     
38     /**
39      * Turns the valve on. This method blocks until the valve is off.
40      * @throws InterruptedException
41      */

42     public void turnOn() throws InterruptedException JavaDoc {
43         synchronized(mutex) {
44             while( on ) {
45                 mutex.wait();
46             }
47             on=true;
48             mutex.notifyAll();
49         }
50     }
51
52     boolean isOn() {
53         synchronized(mutex) {
54             return on;
55         }
56     }
57     
58     /**
59      * Turns the valve off. This method blocks until the valve is on and the valve is not
60      * in use.
61      *
62      * @throws InterruptedException
63      */

64     public void turnOff() throws InterruptedException JavaDoc {
65          synchronized(mutex) {
66              try {
67                  ++turningOff;
68                  while( usage > 0 || !on) {
69                      mutex.wait();
70                  }
71                  on=false;
72                  mutex.notifyAll();
73              } finally {
74                  --turningOff;
75              }
76          }
77     }
78     
79     /**
80      * Increments the use counter of the valve. This method blocks if the valve is off,
81      * or is being turned off.
82      *
83      * @throws InterruptedException
84      */

85     public void increment() throws InterruptedException JavaDoc {
86         synchronized(mutex) {
87             // Do we have to wait for the value to be on?
88
while( turningOff>0 || !on ) {
89                 mutex.wait();
90             }
91             usage++;
92         }
93     }
94     
95     /**
96      * Decrements the use counter of the valve.
97      */

98     public void decrement() {
99         synchronized(mutex) {
100             usage--;
101             if( turningOff>0 && usage < 1 ) {
102                 mutex.notifyAll();
103             }
104         }
105     }
106     
107 }
108
Popular Tags