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


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

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

36   public WaitableShort(short initialValue, Object lock) {
37     super(initialValue, lock);
38   }
39
40
41   public short set(short newValue) {
42     synchronized (lock_) {
43       lock_.notifyAll();
44       return super.set(newValue);
45     }
46   }
47
48   public boolean commit(short assumedValue, short newValue) {
49     synchronized (lock_) {
50       boolean success = super.commit(assumedValue, newValue);
51       if (success) lock_.notifyAll();
52       return success;
53     }
54   }
55
56   public short increment() {
57     synchronized (lock_) {
58       lock_.notifyAll();
59       return super.increment();
60     }
61   }
62
63   public short decrement() {
64     synchronized (lock_) {
65       lock_.notifyAll();
66       return super.decrement();
67     }
68   }
69
70   public short add(short amount) {
71     synchronized (lock_) {
72       lock_.notifyAll();
73       return super.add(amount);
74     }
75   }
76
77   public short subtract(short amount) {
78     synchronized (lock_) {
79       lock_.notifyAll();
80       return super.subtract(amount);
81     }
82   }
83
84   public short multiply(short factor) {
85     synchronized (lock_) {
86       lock_.notifyAll();
87       return super.multiply(factor);
88     }
89   }
90
91   public short divide(short 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(short c, Runnable action) throws InterruptedException {
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(short c, Runnable action) throws InterruptedException {
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(short c, Runnable action) throws InterruptedException {
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(short c, Runnable action) throws InterruptedException {
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(short c, Runnable action) throws InterruptedException {
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(short c, Runnable action) throws InterruptedException {
160     synchronized (lock_) {
161       while (!(value_ > c)) lock_.wait();
162       if (action != null) action.run();
163     }
164   }
165
166 }
167
168

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