KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 */

13
14 package EDU.oswego.cs.dl.util.concurrent;
15
16 /**
17  * A class useful for offloading waiting and signalling operations
18  * on single long 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 WaitableLong extends SynchronizedLong {
24   /**
25    * Make a new WaitableLong with the given initial value,
26    * and using its own internal lock.
27    **/

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

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

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

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

126   public void whenLessEqual(long c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
127     synchronized (lock_) {
128       while (!(value_ <= c)) lock_.wait();
129       if (action != null) action.run();
130     }
131   }
132
133   /**
134    * wait until value less than c, then run action if nonnull.
135    * The action is run with the synchronization lock held.
136    **/

137   public void whenLess(long c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
138     synchronized (lock_) {
139       while (!(value_ < c)) lock_.wait();
140       if (action != null) action.run();
141     }
142   }
143
144   /**
145    * wait until value greater than or equal to c, then run action if nonnull.
146    * The action is run with the synchronization lock held.
147    **/

148   public void whenGreaterEqual(long c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
149     synchronized (lock_) {
150       while (!(value_ >= c)) lock_.wait();
151       if (action != null) action.run();
152     }
153   }
154
155   /**
156    * wait until value greater than c, then run action if nonnull.
157    * The action is run with the synchronization lock held.
158    **/

159   public void whenGreater(long c, Runnable JavaDoc action) throws InterruptedException JavaDoc {
160     synchronized (lock_) {
161       while (!(value_ > c)) lock_.wait();
162       if (action != null) action.run();
163     }
164   }
165
166 }
167
168
Popular Tags