1 14 15 package EDU.oswego.cs.dl.util.concurrent; 16 17 22 23 public class SynchronizedShort extends SynchronizedVariable implements Comparable , Cloneable { 24 25 protected short value_; 26 27 31 public SynchronizedShort(short initialValue) { 32 super(); 33 value_ = initialValue; 34 } 35 36 40 public SynchronizedShort(short initialValue, Object lock) { 41 super(lock); 42 value_ = initialValue; 43 } 44 45 48 public final short get() { synchronized(lock_) { return value_; } } 49 50 54 55 public short set(short newValue) { 56 synchronized (lock_) { 57 short old = value_; 58 value_ = newValue; 59 return old; 60 } 61 } 62 63 67 public boolean commit(short assumedValue, short newValue) { 68 synchronized(lock_) { 69 boolean success = (assumedValue == value_); 70 if (success) value_ = newValue; 71 return success; 72 } 73 } 74 75 76 85 86 public short swap(SynchronizedShort other) { 87 if (other == this) return get(); 88 SynchronizedShort fst = this; 89 SynchronizedShort snd = other; 90 if (System.identityHashCode(fst) > System.identityHashCode(snd)) { 91 fst = other; 92 snd = this; 93 } 94 synchronized(fst.lock_) { 95 synchronized(snd.lock_) { 96 fst.set(snd.set(fst.get())); 97 return get(); 98 } 99 } 100 } 101 102 103 107 public short increment() { 108 synchronized (lock_) { 109 return ++value_; 110 } 111 } 112 113 117 public short decrement() { 118 synchronized (lock_) { 119 return --value_; 120 } 121 } 122 123 127 public short add(short amount) { 128 synchronized (lock_) { 129 return value_ += amount; 130 } 131 } 132 133 137 public short subtract(short amount) { 138 synchronized (lock_) { 139 return value_ -= amount; 140 } 141 } 142 143 147 public short multiply(short factor) { 148 synchronized (lock_) { 149 return value_ *= factor; 150 } 151 } 152 153 157 public short divide(short factor) { 158 synchronized (lock_) { 159 return value_ /= factor; 160 } 161 } 162 163 167 public short negate() { 168 synchronized (lock_) { 169 value_ = (short)(-value_); 170 return value_; 171 } 172 } 173 174 178 public short complement() { 179 synchronized (lock_) { 180 value_ = (short)(~value_); 181 return value_; 182 } 183 } 184 185 189 public short and(short b) { 190 synchronized (lock_) { 191 value_ = (short)(value_ & b); 192 return value_; 193 } 194 } 195 196 200 public short or(short b) { 201 synchronized (lock_) { 202 value_ = (short)(value_ | b); 203 return value_; 204 } 205 } 206 207 208 212 public short xor(short b) { 213 synchronized (lock_) { 214 value_ = (short)(value_ ^ b); 215 return value_; 216 } 217 } 218 219 public int compareTo(short other) { 220 short val = get(); 221 return (val < other)? -1 : (val == other)? 0 : 1; 222 } 223 224 public int compareTo(SynchronizedShort other) { 225 return compareTo(other.get()); 226 } 227 228 public int compareTo(Object other) { 229 return compareTo((SynchronizedShort)other); 230 } 231 232 public boolean equals(Object other) { 233 if (other != null && 234 other instanceof SynchronizedShort) 235 return get() == ((SynchronizedShort)other).get(); 236 else 237 return false; 238 } 239 240 public int hashCode() { 241 return (int)(get()); 242 } 243 244 public String toString() { return String.valueOf(get()); } 245 246 } 247 248 | Popular Tags |