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


1 /*
2   File: WaitableLong.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   13may2004 dl Add notifying bit ops
13 */

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

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

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

37   public WaitableLong(long initialValue, Object lock) {
38     super(initialValue, lock);
39   }
40
41
42   public long set(long newValue) {
43     synchronized (lock_) {
44       lock_.notifyAll();
45       return super.set(newValue);
46     }
47   }
48
49   public boolean commit(long assumedValue, long newValue) {
50     synchronized (lock_) {
51       boolean success = super.commit(assumedValue, newValue);
52       if (success) lock_.notifyAll();
53       return success;
54     }
55   }
56
57   public long increment() {
58     synchronized (lock_) {
59       lock_.notifyAll();
60       return super.increment();
61     }
62   }
63
64   public long decrement() {
65     synchronized (lock_) {
66       lock_.notifyAll();
67       return super.decrement();
68     }
69   }
70
71   public long add(long amount) {
72     synchronized (lock_) {
73       lock_.notifyAll();
74       return super.add(amount);
75     }
76   }
77
78   public long subtract(long amount) {
79     synchronized (lock_) {
80       lock_.notifyAll();
81       return super.subtract(amount);
82     }
83   }
84
85   public long multiply(long factor) {
86     synchronized (lock_) {
87       lock_.notifyAll();
88       return super.multiply(factor);
89     }
90   }
91
92   public long divide(long factor) {
93     synchronized (lock_) {
94       lock_.notifyAll();
95       return super.divide(factor);
96     }
97   }
98
99   /**
100    * Set the value to its complement
101    * @return the new value
102    **/

103   public long complement() {
104     synchronized (lock_) {
105       value_ = ~value_;
106       lock_.notifyAll();
107       return value_;
108     }
109   }
110
111   /**
112    * Set value to value &amp; b.
113    * @return the new value
114    **/

115   public long and(long b) {
116     synchronized (lock_) {
117       value_ = value_ & b;
118       lock_.notifyAll();
119       return value_;
120     }
121   }
122
123   /**
124    * Set value to value | b.
125    * @return the new value
126    **/

127   public long or(long b) {
128     synchronized (lock_) {
129       value_ = value_ | b;
130       lock_.notifyAll();
131       return value_;
132     }
133   }
134
135
136   /**
137    * Set value to value ^ b.
138    * @return the new value
139    **/

140   public long xor(long b) {
141     synchronized (lock_) {
142       value_ = value_ ^ b;
143       lock_.notifyAll();
144       return value_;
145     }
146   }
147
148
149   /**
150    * Wait until value equals c, then run action if nonnull.
151    * The action is run with the synchronization lock held.
152    **/

153
154   public void whenEqual(long c, Runnable action) throws InterruptedException {
155     synchronized(lock_) {
156       while (!(value_ == c)) lock_.wait();
157       if (action != null) action.run();
158     }
159   }
160
161   /**
162    * wait until value not equal to c, then run action if nonnull.
163    * The action is run with the synchronization lock held.
164    **/

165   public void whenNotEqual(long c, Runnable action) throws InterruptedException {
166     synchronized (lock_) {
167       while (!(value_ != c)) lock_.wait();
168       if (action != null) action.run();
169     }
170   }
171
172   /**
173    * wait until value less than or equal to c, then run action if nonnull.
174    * The action is run with the synchronization lock held.
175    **/

176   public void whenLessEqual(long c, Runnable action) throws InterruptedException {
177     synchronized (lock_) {
178       while (!(value_ <= c)) lock_.wait();
179       if (action != null) action.run();
180     }
181   }
182
183   /**
184    * wait until value less than c, then run action if nonnull.
185    * The action is run with the synchronization lock held.
186    **/

187   public void whenLess(long c, Runnable action) throws InterruptedException {
188     synchronized (lock_) {
189       while (!(value_ < c)) lock_.wait();
190       if (action != null) action.run();
191     }
192   }
193
194   /**
195    * wait until value greater than or equal to c, then run action if nonnull.
196    * The action is run with the synchronization lock held.
197    **/

198   public void whenGreaterEqual(long c, Runnable action) throws InterruptedException {
199     synchronized (lock_) {
200       while (!(value_ >= c)) lock_.wait();
201       if (action != null) action.run();
202     }
203   }
204
205   /**
206    * wait until value greater than c, then run action if nonnull.
207    * The action is run with the synchronization lock held.
208    **/

209   public void whenGreater(long c, Runnable action) throws InterruptedException {
210     synchronized (lock_) {
211       while (!(value_ > c)) lock_.wait();
212       if (action != null) action.run();
213     }
214   }
215
216 }
217
218

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