Mercurial > hg > blitz_condensed
comparison src/EDU/oswego/cs/dl/util/concurrent/SynchronizedLong.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* | |
2 File: SynchronizedLong.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 long 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 SynchronizedLong extends SynchronizedVariable implements Comparable, Cloneable { | |
23 | |
24 protected long value_; | |
25 | |
26 /** | |
27 * Make a new SynchronizedLong with the given initial value, | |
28 * and using its own internal lock. | |
29 **/ | |
30 public SynchronizedLong(long initialValue) { | |
31 super(); | |
32 value_ = initialValue; | |
33 } | |
34 | |
35 /** | |
36 * Make a new SynchronizedLong with the given initial value, | |
37 * and using the supplied lock. | |
38 **/ | |
39 public SynchronizedLong(long initialValue, Object lock) { | |
40 super(lock); | |
41 value_ = initialValue; | |
42 } | |
43 | |
44 /** | |
45 * Return the current value | |
46 **/ | |
47 public final long get() { synchronized(lock_) { return value_; } } | |
48 | |
49 /** | |
50 * Set to newValue. | |
51 * @return the old value | |
52 **/ | |
53 | |
54 public long set(long newValue) { | |
55 synchronized (lock_) { | |
56 long 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(long assumedValue, long newValue) { | |
67 synchronized(lock_) { | |
68 boolean success = (assumedValue == value_); | |
69 if (success) value_ = newValue; | |
70 return success; | |
71 } | |
72 } | |
73 | |
74 /** | |
75 * Atomically swap values with another SynchronizedLong. | |
76 * Uses identityHashCode to avoid deadlock when | |
77 * two SynchronizedLongs attempt to simultaneously swap with each other. | |
78 * @return the new value | |
79 **/ | |
80 | |
81 public long swap(SynchronizedLong other) { | |
82 if (other != this) { | |
83 SynchronizedLong fst = this; | |
84 SynchronizedLong snd = other; | |
85 if (System.identityHashCode(fst) > System.identityHashCode(snd)) { | |
86 fst = other; | |
87 snd = this; | |
88 } | |
89 synchronized(fst.lock_) { | |
90 synchronized(snd.lock_) { | |
91 fst.set(snd.set(fst.get())); | |
92 } | |
93 } | |
94 } | |
95 return value_; | |
96 } | |
97 | |
98 /** | |
99 * Increment the value. | |
100 * @return the new value | |
101 **/ | |
102 public long increment() { | |
103 synchronized (lock_) { | |
104 return ++value_; | |
105 } | |
106 } | |
107 | |
108 /** | |
109 * Decrement the value. | |
110 * @return the new value | |
111 **/ | |
112 public long decrement() { | |
113 synchronized (lock_) { | |
114 return --value_; | |
115 } | |
116 } | |
117 | |
118 /** | |
119 * Add amount to value (i.e., set value += amount) | |
120 * @return the new value | |
121 **/ | |
122 public long add(long amount) { | |
123 synchronized (lock_) { | |
124 return value_ += amount; | |
125 } | |
126 } | |
127 | |
128 /** | |
129 * Subtract amount from value (i.e., set value -= amount) | |
130 * @return the new value | |
131 **/ | |
132 public long subtract(long amount) { | |
133 synchronized (lock_) { | |
134 return value_ -= amount; | |
135 } | |
136 } | |
137 | |
138 /** | |
139 * Multiply value by factor (i.e., set value *= factor) | |
140 * @return the new value | |
141 **/ | |
142 public synchronized long multiply(long factor) { | |
143 synchronized (lock_) { | |
144 return value_ *= factor; | |
145 } | |
146 } | |
147 | |
148 /** | |
149 * Divide value by factor (i.e., set value /= factor) | |
150 * @return the new value | |
151 **/ | |
152 public long divide(long factor) { | |
153 synchronized (lock_) { | |
154 return value_ /= factor; | |
155 } | |
156 } | |
157 | |
158 /** | |
159 * Set the value to the negative of its old value | |
160 * @return the new value | |
161 **/ | |
162 public long negate() { | |
163 synchronized (lock_) { | |
164 value_ = -value_; | |
165 return value_; | |
166 } | |
167 } | |
168 | |
169 /** | |
170 * Set the value to its complement | |
171 * @return the new value | |
172 **/ | |
173 public long complement() { | |
174 synchronized (lock_) { | |
175 value_ = ~value_; | |
176 return value_; | |
177 } | |
178 } | |
179 | |
180 /** | |
181 * Set value to value & b. | |
182 * @return the new value | |
183 **/ | |
184 public long and(long b) { | |
185 synchronized (lock_) { | |
186 value_ = value_ & b; | |
187 return value_; | |
188 } | |
189 } | |
190 | |
191 /** | |
192 * Set value to value | b. | |
193 * @return the new value | |
194 **/ | |
195 public long or(long b) { | |
196 synchronized (lock_) { | |
197 value_ = value_ | b; | |
198 return value_; | |
199 } | |
200 } | |
201 | |
202 | |
203 /** | |
204 * Set value to value ^ b. | |
205 * @return the new value | |
206 **/ | |
207 public long xor(long b) { | |
208 synchronized (lock_) { | |
209 value_ = value_ ^ b; | |
210 return value_; | |
211 } | |
212 } | |
213 | |
214 | |
215 public int compareTo(long other) { | |
216 long val = get(); | |
217 return (val < other)? -1 : (val == other)? 0 : 1; | |
218 } | |
219 | |
220 public int compareTo(SynchronizedLong other) { | |
221 return compareTo(other.get()); | |
222 } | |
223 | |
224 public int compareTo(Object other) { | |
225 return compareTo((SynchronizedLong)other); | |
226 } | |
227 | |
228 public boolean equals(Object other) { | |
229 if (other != null && | |
230 other instanceof SynchronizedLong) | |
231 return get() == ((SynchronizedLong)other).get(); | |
232 else | |
233 return false; | |
234 } | |
235 | |
236 public int hashCode() { // same expression as java.lang.Long | |
237 long v = get(); | |
238 return (int)(v ^ (v >> 32)); | |
239 } | |
240 | |
241 public String toString() { return String.valueOf(get()); } | |
242 | |
243 } | |
244 |