| ||||
|
Code - Class EDU.oswego.cs.dl.util.concurrent.SynchronizedInt1 /* 2 File: SynchronizedInt.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 15Apr2003 dl Removed redundant "synchronized" for multiply() 13 */ 14 15 package EDU.oswego.cs.dl.util.concurrent; 16 17 /** 18 * A class useful for offloading synch for int instance variables. 19 * 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 SynchronizedInt extends SynchronizedVariable implements Comparable, Cloneable { 24 25 protected int value_; 26 27 /** 28 * Make a new SynchronizedInt with the given initial value, 29 * and using its own internal lock. 30 **/ 31 public SynchronizedInt(int initialValue) { 32 super(); 33 value_ = initialValue; 34 } 35 36 /** 37 * Make a new SynchronizedInt with the given initial value, 38 * and using the supplied lock. 39 **/ 40 public SynchronizedInt(int initialValue, Object lock) { 41 super(lock); 42 value_ = initialValue; 43 } 44 45 /** 46 * Return the current value 47 **/ 48 public final int get() { synchronized(lock_) { return value_; } } 49 50 /** 51 * Set to newValue. 52 * @return the old value 53 **/ 54 55 public int set(int newValue) { 56 synchronized (lock_) { 57 int old = value_; 58 value_ = newValue; 59 return old; 60 } 61 } 62 63 /** 64 * Set value to newValue only if it is currently assumedValue. 65 * @return true if successful 66 **/ 67 public boolean commit(int assumedValue, int newValue) { 68 synchronized(lock_) { 69 boolean success = (assumedValue == value_); 70 if (success) value_ = newValue; 71 return success; 72 } 73 } 74 75 /** 76 * Atomically swap values with another SynchronizedInt. 77 * Uses identityHashCode to avoid deadlock when 78 * two SynchronizedInts 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 int swap(SynchronizedInt other) { 86 if (other == this) return get(); 87 SynchronizedInt fst = this; 88 SynchronizedInt 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 * Increment the value. 103 * @return the new value 104 **/ 105 public int increment() { 106 synchronized (lock_) { 107 return ++value_; 108 } 109 } 110 111 /** 112 * Decrement the value. 113 * @return the new value 114 **/ 115 public int decrement() { 116 synchronized (lock_) { 117 return --value_; 118 } 119 } 120 121 /** 122 * Add amount to value (i.e., set value += amount) 123 * @return the new value 124 **/ 125 public int add(int amount) { 126 synchronized (lock_) { 127 return value_ += amount; 128 } 129 } 130 131 /** 132 * Subtract amount from value (i.e., set value -= amount) 133 * @return the new value 134 **/ 135 public int subtract(int amount) { 136 synchronized (lock_) { 137 return value_ -= amount; 138 } 139 } 140 141 /** 142 * Multiply value by factor (i.e., set value *= factor) 143 * @return the new value 144 **/ 145 public int multiply(int factor) { 146 synchronized (lock_) { 147 return value_ *= factor; 148 } 149 } 150 151 /** 152 * Divide value by factor (i.e., set value /= factor) 153 * @return the new value 154 **/ 155 public int divide(int factor) { 156 synchronized (lock_) { 157 return value_ /= factor; 158 } 159 } 160 161 /** 162 * Set the value to the negative of its old value 163 * @return the new value 164 **/ 165 public int negate() { 166 synchronized (lock_) { 167 value_ = -value_; 168 return value_; 169 } 170 } 171 172 /** 173 * Set the value to its complement 174 * @return the new value 175 **/ 176 public int complement() { 177 synchronized (lock_) { 178 value_ = ~value_; 179 return value_; 180 } 181 } 182 183 /** 184 * Set value to value & b. 185 * @return the new value 186 **/ 187 public int and(int b) { 188 synchronized (lock_) { 189 value_ = value_ & b; 190 return value_; 191 } 192 } 193 194 /** 195 * Set value to value | b. 196 * @return the new value 197 **/ 198 public int or(int b) { 199 synchronized (lock_) { 200 value_ = value_ | b; 201 return value_; 202 } 203 } 204 205 206 /** 207 * Set value to value ^ b. 208 * @return the new value 209 **/ 210 public int xor(int b) { 211 synchronized (lock_) { 212 value_ = value_ ^ b; 213 return value_; 214 } 215 } 216 217 public int compareTo(int other) { 218 int val = get(); 219 return (val < other)? -1 : (val == other)? 0 : 1; 220 } 221 222 public int compareTo(SynchronizedInt other) { 223 return compareTo(other.get()); 224 } 225 226 public int compareTo(Object other) { 227 return compareTo((SynchronizedInt)other); 228 } 229 230 public boolean equals(Object other) { 231 if (other != null && 232 other instanceof SynchronizedInt) 233 return get() == ((SynchronizedInt)other).get(); 234 else 235 return false; 236 } 237 238 public int hashCode() { return get(); } 239 240 public String toString() { return String.valueOf(get()); } 241 242 } 243 244 |
|||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|