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


1 /*
2   File: SynchronizedShort.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   19Jun1998 dl Create public version
12 */

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

21
22 public class SynchronizedShort extends SynchronizedVariable implements Comparable, Cloneable {
23
24   protected short value_;
25
26   /**
27    * Make a new SynchronizedShort with the given initial value,
28    * and using its own internal lock.
29    **/

30   public SynchronizedShort(short initialValue) {
31     super();
32     value_ = initialValue;
33   }
34
35   /**
36    * Make a new SynchronizedShort with the given initial value,
37    * and using the supplied lock.
38    **/

39   public SynchronizedShort(short initialValue, Object lock) {
40     super(lock);
41     value_ = initialValue;
42   }
43
44   /**
45    * Return the current value
46    **/

47   public final short get() { synchronized(lock_) { return value_; } }
48
49   /**
50    * Set to newValue.
51    * @return the old value
52    **/

53
54   public short set(short newValue) {
55     synchronized (lock_) {
56       short old = value_;
57       value_ = newValue;
58       return old;
59     }
60   }
61
62   /**
63    * Set value to newValue only if it is currently assumedValue.
64    * @return true if successful
65    **/

66   public boolean commit(short assumedValue, short newValue) {
67     synchronized(lock_) {
68       boolean success = (assumedValue == value_);
69       if (success) value_ = newValue;
70       return success;
71     }
72   }
73
74
75   /**
76    * Atomically swap values with another SynchronizedShort.
77    * Uses identityHashCode to avoid deadlock when
78    * two SynchronizedShorts attempt to simultaneously swap with each other.
79    * (Note: Ordering via identyHashCode is not strictly guaranteed
80    * by the language specification to return unique, orderable
81    * values, but in practice JVMs rely on them being unique.)
82    * @return the new value
83    **/

84
85   public short swap(SynchronizedShort other) {
86     if (other == this) return get();
87     SynchronizedShort fst = this;
88     SynchronizedShort snd = other;
89     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
90       fst = other;
91       snd = this;
92     }
93     synchronized(fst.lock_) {
94       synchronized(snd.lock_) {
95         fst.set(snd.set(fst.get()));
96         return get();
97       }
98     }
99   }
100
101
102   /**
103    * Increment the value.
104    * @return the new value
105    **/

106   public short increment() {
107     synchronized (lock_) {
108       return ++value_;
109     }
110   }
111
112   /**
113    * Decrement the value.
114    * @return the new value
115    **/

116   public short decrement() {
117     synchronized (lock_) {
118       return --value_;
119     }
120   }
121
122   /**
123    * Add amount to value (i.e., set value += amount)
124    * @return the new value
125    **/

126   public short add(short amount) {
127     synchronized (lock_) {
128       return value_ += amount;
129     }
130   }
131
132   /**
133    * Subtract amount from value (i.e., set value -= amount)
134    * @return the new value
135    **/

136   public short subtract(short amount) {
137     synchronized (lock_) {
138       return value_ -= amount;
139     }
140   }
141
142   /**
143    * Multiply value by factor (i.e., set value *= factor)
144    * @return the new value
145    **/

146   public synchronized short multiply(short factor) {
147     synchronized (lock_) {
148       return value_ *= factor;
149     }
150   }
151
152   /**
153    * Divide value by factor (i.e., set value /= factor)
154    * @return the new value
155    **/

156   public short divide(short factor) {
157     synchronized (lock_) {
158       return value_ /= factor;
159     }
160   }
161
162   /**
163    * Set the value to the negative of its old value
164    * @return the new value
165    **/

166   public short negate() {
167     synchronized (lock_) {
168       value_ = (short)(-value_);
169       return value_;
170     }
171   }
172
173   /**
174    * Set the value to its complement
175    * @return the new value
176    **/

177   public short complement() {
178     synchronized (lock_) {
179       value_ = (short)(~value_);
180       return value_;
181     }
182   }
183
184   /**
185    * Set value to value &amp; b.
186    * @return the new value
187    **/

188   public short and(short b) {
189     synchronized (lock_) {
190       value_ = (short)(value_ & b);
191       return value_;
192     }
193   }
194
195   /**
196    * Set value to value | b.
197    * @return the new value
198    **/

199   public short or(short b) {
200     synchronized (lock_) {
201       value_ = (short)(value_ | b);
202       return value_;
203     }
204   }
205
206
207   /**
208    * Set value to value ^ b.
209    * @return the new value
210    **/

211   public short xor(short b) {
212     synchronized (lock_) {
213       value_ = (short)(value_ ^ b);
214       return value_;
215     }
216   }
217
218   public int compareTo(short other) {
219     short val = get();
220     return (val < other)? -1 : (val == other)? 0 : 1;
221   }
222
223   public int compareTo(SynchronizedShort other) {
224     return compareTo(other.get());
225   }
226
227   public int compareTo(Object other) {
228     return compareTo((SynchronizedShort)other);
229   }
230
231   public boolean equals(Object other) {
232     if (other != null &&
233         other instanceof SynchronizedShort)
234       return get() == ((SynchronizedShort)other).get();
235     else
236       return false;
237   }
238
239   public int hashCode() {
240     return (int)(get());
241   }
242
243   public String toString() { return String.valueOf(get()); }
244
245 }
246
247

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