KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > EDU > oswego > cs > dl > util > concurrent > WaitableFloat


1 /*
2   File: WaitableFloat.java
3
4   Originally written by Doug Lea and released into the public domain.
5   This may be used for any purposes whatsoever without acknowledgment.
6   Thanks for the assistance and support of Sun Microsystems Labs,
7   and everyone contributing, testing, and using this code.
8
9   History:
10   Date Who What
11   23Jun1998 dl Create public version
12 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15
16 /**
17  * A class useful for offloading waiting and signalling operations
18  * on single float variables.
19  * <p>
20  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
21  **/

22
23 public class WaitableFloat extends SynchronizedFloat {
24   /**
25    * Make a new WaitableFloat with the given initial value,
26    * and using its own internal lock.
27    **/

28   public WaitableFloat(float initialValue) {
29     super(initialValue);
30   }
31
32   /**
33    * Make a new WaitableFloat with the given initial value,
34    * and using the supplied lock.
35    **/

36   public WaitableFloat(float initialValue, Object JavaDoc lock) {
37     super(initialValue, lock);
38   }
39
40
41   public float set(float newValue) {
42     synchronized (lock_) {
43       lock_.notifyAll();
44       return super.set(newValue);
45     }
46   }
47
48   public boolean commit(float assumedValue, float newValue) {
49     synchronized (lock_) {
50       boolean success = super.commit(assumedValue, newValue);
51       if (success) lock_.notifyAll();
52       return success;
53     }
54   }
55
56
57   public float add(float amount) {
58     synchronized (lock_) {
59       lock_.notifyAll();
60       return super.add(amount);
61     }
62   }
63
64   public float subtract(float amount) {
65     synchronized (lock_) {
66       lock_.notifyAll();
67       return super.subtract(amount);
68     }
69   }
70
71   public float multiply(float factor) {
72     synchronized (lock_) {
73       lock_.notifyAll();
74       return super.multiply(factor);
75     }
76   }
77
78   public float divide(float factor) {
79     synchronized (lock_) {
80       lock_.notifyAll();
81       return super.divide(factor);
82     }
83   }
84
85
86   /**
87    * Wait until value equals c, then run action if nonnull.
88    * The action is run with the synchronization lock held.
89    **/

90
91   public void whenEqual(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
92     synchronized(lock_) {
93       while (!(value_ == c)) lock_.wait();
94       if (action != null) action.run();
95     }
96   }
97
98   /**
99    * wait until value not equal to c, then run action if nonnull.
100    * The action is run with the synchronization lock held.
101    **/

102   public void whenNotEqual(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
103     synchronized (lock_) {
104       while (!(value_ != c)) lock_.wait();
105       if (action != null) action.run();
106     }
107   }
108
109   /**
110    * wait until value less than or equal to c, then run action if nonnull.
111    * The action is run with the synchronization lock held.
112    **/

113   public void whenLessEqual(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
114     synchronized (lock_) {
115       while (!(value_ <= c)) lock_.wait();
116       if (action != null) action.run();
117     }
118   }
119
120   /**
121    * wait until value less than c, then run action if nonnull.
122    * The action is run with the synchronization lock held.
123    **/

124   public void whenLess(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
125     synchronized (lock_) {
126       while (!(value_ < c)) lock_.wait();
127       if (action != null) action.run();
128     }
129   }
130
131   /**
132    * wait until value greater than or equal to c, then run action if nonnull.
133    * The action is run with the synchronization lock held.
134    **/

135   public void whenGreaterEqual(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
136     synchronized (lock_) {
137       while (!(value_ >= c)) lock_.wait();
138       if (action != null) action.run();
139     }
140   }
141
142   /**
143    * wait until value greater than c, then run action if nonnull.
144    * The action is run with the synchronization lock held.
145    **/

146   public void whenGreater(float c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
147     synchronized (lock_) {
148       while (!(value_ > c)) lock_.wait();
149       if (action != null) action.run();
150     }
151   }
152
153 }
154
155
Popular Tags