Code - Class EDU.oswego.cs.dl.util.concurrent.WaitableDouble


1 /*
2   File: WaitableDouble.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 double 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 WaitableDouble extends SynchronizedDouble {
24   /**
25    * Make a new WaitableDouble with the given initial value,
26    * and using its own internal lock.
27    **/

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

36   public WaitableDouble(double initialValue, Object lock) {
37     super(initialValue, lock);
38   }
39
40
41   public double set(double newValue) {
42     synchronized (lock_) {
43       lock_.notifyAll();
44       return super.set(newValue);
45     }
46   }
47
48   public boolean commit(double assumedValue, double 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 double add(double amount) {
58     synchronized (lock_) {
59       lock_.notifyAll();
60       return super.add(amount);
61     }
62   }
63
64   public double subtract(double amount) {
65     synchronized (lock_) {
66       lock_.notifyAll();
67       return super.subtract(amount);
68     }
69   }
70
71   public double multiply(double factor) {
72     synchronized (lock_) {
73       lock_.notifyAll();
74       return super.multiply(factor);
75     }
76   }
77
78   public double divide(double 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(double c, Runnable action) throws InterruptedException {
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(double c, Runnable action) throws InterruptedException {
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(double c, Runnable action) throws InterruptedException {
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(double c, Runnable action) throws InterruptedException {
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(double c, Runnable action) throws InterruptedException {
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(double c, Runnable action) throws InterruptedException {
147     synchronized (lock_) {
148       while (!(value_ > c)) lock_.wait();
149       if (action != null) action.run();
150     }
151   }
152
153 }
154
155

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates