Code - Class EDU.oswego.cs.dl.util.concurrent.SynchronizedDouble


1 /*
2   File: SynchronizedDouble.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 double 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 SynchronizedDouble extends SynchronizedVariable implements Comparable, Cloneable {
23
24   protected double value_;
25
26   /**
27    * Make a new SynchronizedDouble with the given initial value,
28    * and using its own internal lock.
29    **/

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

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

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

53
54   public double set(double newValue) {
55     synchronized (lock_) {
56       double 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(double assumedValue, double newValue) {
67     synchronized(lock_) {
68       boolean success = (assumedValue == value_);
69       if (success) value_ = newValue;
70       return success;
71     }
72   }
73
74
75   /**
76    * Atomically swap values with another SynchronizedDouble.
77    * Uses identityHashCode to avoid deadlock when
78    * two SynchronizedDoubles 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 double swap(SynchronizedDouble other) {
86     if (other == this) return get();
87     SynchronizedDouble fst = this;
88     SynchronizedDouble 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   /**
103    * Add amount to value (i.e., set value += amount)
104    * @return the new value
105    **/

106   public double add(double amount) {
107     synchronized (lock_) {
108       return value_ += amount;
109     }
110   }
111
112   /**
113    * Subtract amount from value (i.e., set value -= amount)
114    * @return the new value
115    **/

116   public double subtract(double amount) {
117     synchronized (lock_) {
118       return value_ -= amount;
119     }
120   }
121
122   /**
123    * Multiply value by factor (i.e., set value *= factor)
124    * @return the new value
125    **/

126   public synchronized double multiply(double factor) {
127     synchronized (lock_) {
128       return value_ *= factor;
129     }
130   }
131
132   /**
133    * Divide value by factor (i.e., set value /= factor)
134    * @return the new value
135    **/

136   public double divide(double factor) {
137     synchronized (lock_) {
138       return value_ /= factor;
139     }
140   }
141
142   public int compareTo(double other) {
143     double val = get();
144     return (val < other)? -1 : (val == other)? 0 : 1;
145   }
146
147   public int compareTo(SynchronizedDouble other) {
148     return compareTo(other.get());
149   }
150
151   public int compareTo(Object other) {
152     return compareTo((SynchronizedDouble)other);
153   }
154
155   public boolean equals(Object other) {
156     if (other != null &&
157         other instanceof SynchronizedDouble)
158       return get() == ((SynchronizedDouble)other).get();
159     else
160       return false;
161   }
162
163   public int hashCode() { // same hash as Double
164
long bits = Double.doubleToLongBits(get());
165     return (int)(bits ^ (bits >> 32));
166   }
167
168   public String toString() { return String.valueOf(get()); }
169
170 }
171
172

Java API By Example, From Geeks To Geeks. | Conditions of Use | About Us © 2002 - 2005, KickJava.com, or its affiliates