View Javadoc

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.jdiagnose.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, Cloneable {
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 lock) { 
40      super(lock); 
41      value_ = initialValue; 
42    }
43  
44    /*** 
45     * Return the current value 
46     **/
47    public final int get() { synchronized(lock_) { return value_; } }
48  
49    /*** 
50     * Set to newValue.
51     * @return the old value 
52     **/
53  
54    public int set(int newValue) { 
55      synchronized (lock_) {
56        int 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(int assumedValue, int 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 SynchronizedInt.
76     * Uses identityHashCode to avoid deadlock when
77     * two SynchronizedInts attempt to simultaneously swap with each other.
78     * (Note: Ordering via identyHashCode is not strictly guaranteed
79     * by the language specification to return unique, orderable
80     * values, but in practice JVMs rely on them being unique.)
81     * @return the new value 
82     **/
83  
84    public int swap(SynchronizedInt other) {
85      if (other == this) return get();
86      SynchronizedInt fst = this;
87      SynchronizedInt snd = other;
88      if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
89        fst = other;
90        snd = this;
91      }
92      synchronized(fst.lock_) {
93        synchronized(snd.lock_) {
94          fst.set(snd.set(fst.get()));
95          return get();
96        }
97      }
98    }
99  
100   /*** 
101    * Increment the value.
102    * @return the new value 
103    **/
104   public int increment() { 
105     synchronized (lock_) {
106       return ++value_; 
107     }
108   }
109 
110   /*** 
111    * Decrement the value.
112    * @return the new value 
113    **/
114   public int decrement() { 
115     synchronized (lock_) {
116       return --value_; 
117     }
118   }
119 
120   /*** 
121    * Add amount to value (i.e., set value += amount)
122    * @return the new value 
123    **/
124   public int add(int amount) { 
125     synchronized (lock_) {
126       return value_ += amount; 
127     }
128   }
129 
130   /*** 
131    * Subtract amount from value (i.e., set value -= amount)
132    * @return the new value 
133    **/
134   public int subtract(int amount) { 
135     synchronized (lock_) {
136       return value_ -= amount; 
137     }
138   }
139 
140   /*** 
141    * Multiply value by factor (i.e., set value *= factor)
142    * @return the new value 
143    **/
144   public synchronized int multiply(int factor) { 
145     synchronized (lock_) {
146       return value_ *= factor; 
147     }
148   }
149 
150   /*** 
151    * Divide value by factor (i.e., set value /= factor)
152    * @return the new value 
153    **/
154   public int divide(int factor) { 
155     synchronized (lock_) {
156       return value_ /= factor; 
157     }
158   }
159 
160   /*** 
161    * Set the value to the negative of its old value
162    * @return the new value 
163    **/
164   public  int negate() { 
165     synchronized (lock_) {
166       value_ = -value_;
167       return value_;
168     }
169   }
170 
171   /*** 
172    * Set the value to its complement
173    * @return the new value 
174    **/
175   public  int complement() { 
176     synchronized (lock_) {
177       value_ = ~value_;
178       return value_;
179     }
180   }
181 
182   /*** 
183    * Set value to value &amp; b.
184    * @return the new value 
185    **/
186   public  int and(int b) { 
187     synchronized (lock_) {
188       value_ = value_ & b;
189       return value_;
190     }
191   }
192 
193   /*** 
194    * Set value to value | b.
195    * @return the new value 
196    **/
197   public  int or(int b) { 
198     synchronized (lock_) {
199       value_ = value_ | b;
200       return value_;
201     }
202   }
203 
204 
205   /*** 
206    * Set value to value ^ b.
207    * @return the new value 
208    **/
209   public  int xor(int b) { 
210     synchronized (lock_) {
211       value_ = value_ ^ b;
212       return value_;
213     }
214   }
215 
216   public int compareTo(int other) {
217     int val = get();
218     return (val < other)? -1 : (val == other)? 0 : 1;
219   }
220 
221   public int compareTo(SynchronizedInt other) {
222     return compareTo(other.get());
223   }
224 
225   public int compareTo(Object other) {
226     return compareTo((SynchronizedInt)other);
227   }
228 
229   public boolean equals(Object other) {
230     if (other != null &&
231         other instanceof SynchronizedInt)
232       return get() == ((SynchronizedInt)other).get();
233     else
234       return false;
235   }
236 
237   public int hashCode() { return get(); }
238 
239   public String toString() { return String.valueOf(get()); }
240 
241 }
242