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


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

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

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

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

53
54   public float set(float newValue) {
55     synchronized (lock_) {
56       float 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(float assumedValue, float 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 SynchronizedFloat.
77    * Uses identityHashCode to avoid deadlock when
78    * two SynchronizedFloats 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 float swap(SynchronizedFloat other) {
86     if (other == this) return get();
87     SynchronizedFloat fst = this;
88     SynchronizedFloat 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 float add(float 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 float subtract(float 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 float multiply(float 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 float divide(float factor) {
137     synchronized (lock_) {
138       return value_ /= factor;
139     }
140   }
141
142   public int compareTo(float other) {
143     float val = get();
144     return (val < other)? -1 : (val == other)? 0 : 1;
145   }
146
147   public int compareTo(SynchronizedFloat other) {
148     return compareTo(other.get());
149   }
150
151   public int compareTo(Object other) {
152     return compareTo((SynchronizedFloat)other);
153   }
154
155   public boolean equals(Object other) {
156     if (other != null &&
157         other instanceof SynchronizedFloat)
158       return get() == ((SynchronizedFloat)other).get();
159     else
160       return false;
161   }
162
163   public int hashCode() {
164     return Float.floatToIntBits(get());
165   }
166
167   public String toString() { return String.valueOf(get()); }
168
169 }
170
171

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