KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > util > CondVar


1 package org.jgroups.util;
2
3 import org.jgroups.TimeoutException;
4
5
6 /**
7  * Class that checks on a condition and - if condition doesn't match the expected result - waits until the result
8  * matches the expected result, or a timeout occurs. First version used WaitableBoolean from util.concurrent, but
9  * that class would not allow for timeouts.
10  * @author Bela Ban
11  * @version $Id: CondVar.java,v 1.3 2004/12/31 14:10:40 belaban Exp $
12  */

13 public class CondVar {
14     Object JavaDoc cond;
15     final String JavaDoc name;
16     final Object JavaDoc lock;
17
18     public CondVar(String JavaDoc name, Object JavaDoc cond) {
19         this.name=name;
20         this.cond=cond;
21         lock=this;
22     }
23
24     public CondVar(String JavaDoc name, Object JavaDoc cond, Object JavaDoc lock) {
25         this.name=name;
26         this.cond=cond;
27         this.lock=lock;
28     }
29
30     public Object JavaDoc get() {
31         synchronized(lock) {
32             return cond;
33         }
34     }
35
36     /** Sets the result */
37     public void set(Object JavaDoc result) {
38         synchronized(lock) {
39             cond=result;
40             lock.notifyAll();
41         }
42     }
43
44     public Object JavaDoc getLock() {
45         return lock;
46     }
47
48
49     /**
50      * Waits until the condition matches the expected result. Returns immediately if they match, otherwise waits
51      * for timeout milliseconds or until the results match.
52      * @param result The result, needs to match the condition (using equals()).
53      * @param timeout Number of milliseconds to wait. A value of <= 0 means to wait forever
54      * @throws TimeoutException Thrown if the result still doesn't match the condition after timeout
55      * milliseconds have elapsed
56      */

57     public void waitUntilWithTimeout(Object JavaDoc result, long timeout) throws TimeoutException {
58         _waitUntilWithTimeout(result, timeout);
59     }
60
61     /**
62      * Waits until the condition matches the expected result. Returns immediately if they match, otherwise waits
63      * for timeout milliseconds or until the results match. This method doesn't throw a TimeoutException
64      * @param result The result, needs to match the condition (using equals()).
65      * @param timeout Number of milliseconds to wait. A value of <= 0 means to wait forever
66      */

67     public void waitUntil(Object JavaDoc result, long timeout) {
68         try {
69             _waitUntilWithTimeout(result, timeout);
70         }
71         catch(TimeoutException e) {
72
73         }
74     }
75
76     public void waitUntil(Object JavaDoc result) {
77         try {waitUntilWithTimeout(result, 0);} catch(TimeoutException e) {}
78     }
79
80
81
82     private void _waitUntilWithTimeout(Object JavaDoc result, long timeout) throws TimeoutException {
83         long time_to_wait=timeout, start;
84         boolean timeout_occurred=false;
85         synchronized(lock) {
86             if(result == null && cond == null) return;
87
88             start=System.currentTimeMillis();
89             while(match(result, cond) == false) {
90                 if(timeout <= 0) {
91                     doWait();
92                 }
93                 else {
94                     if(time_to_wait <= 0) {
95                         timeout_occurred=true;
96                         break; // terminate the while loop
97
}
98                     else {
99                         doWait(time_to_wait);
100                         time_to_wait=timeout - (System.currentTimeMillis() - start);
101                     }
102                 }
103             }
104             if(timeout_occurred)
105                 throw new TimeoutException();
106         }
107     }
108
109
110
111
112     void doWait() {
113         try {lock.wait();} catch(InterruptedException JavaDoc e) {}
114     }
115
116     void doWait(long timeout) {
117         try {lock.wait(timeout);} catch(InterruptedException JavaDoc e) {}
118     }
119
120     private boolean match(Object JavaDoc o1, Object JavaDoc o2) {
121         if(o1 != null)
122             return o1.equals(o2);
123         else
124             return o2.equals(o1);
125     }
126
127     public String JavaDoc toString() {
128         return name + "=" + cond;
129     }
130 }
131
Popular Tags