KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > concurrent > SynchronizedInt


1 /*
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 */

13
14 package org.logicalcobwebs.concurrent;
15
16 /**
17  * A class useful for offloading synch for int 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 SynchronizedInt extends SynchronizedVariable implements Comparable JavaDoc, Cloneable JavaDoc {
23
24     protected int value_;
25
26     /**
27      * Make a new SynchronizedInt with the given initial value,
28      * and using its own internal lock.
29      **/

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

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

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

57
58     public int set(int newValue) {
59         synchronized (lock_) {
60             int old = value_;
61             value_ = newValue;
62             return old;
63         }
64     }
65
66     /**
67      * Set value to newValue only if it is currently assumedValue.
68      * @return true if successful
69      **/

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

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

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

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

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

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

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

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

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

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

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

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

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