KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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   15Apr2003 dl Removed redundant "synchronized" for multiply()
13 */

14
15 package EDU.oswego.cs.dl.util.concurrent;
16
17 /**
18  * A class useful for offloading synch for float instance variables.
19  *
20  * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
21  **/

22
23 public class SynchronizedFloat extends SynchronizedVariable implements Comparable JavaDoc, Cloneable JavaDoc {
24
25   protected float value_;
26
27   /**
28    * Make a new SynchronizedFloat with the given initial value,
29    * and using its own internal lock.
30    **/

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

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

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

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

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

85
86   public float swap(SynchronizedFloat other) {
87     if (other == this) return get();
88     SynchronizedFloat fst = this;
89     SynchronizedFloat snd = other;
90     if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
91       fst = other;
92       snd = this;
93     }
94     synchronized(fst.lock_) {
95       synchronized(snd.lock_) {
96         fst.set(snd.set(fst.get()));
97         return get();
98       }
99     }
100   }
101
102
103   /**
104    * Add amount to value (i.e., set value += amount)
105    * @return the new value
106    **/

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

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

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

137   public float divide(float factor) {
138     synchronized (lock_) {
139       return value_ /= factor;
140     }
141   }
142
143   public int compareTo(float other) {
144     float val = get();
145     return (val < other)? -1 : (val == other)? 0 : 1;
146   }
147
148   public int compareTo(SynchronizedFloat other) {
149     return compareTo(other.get());
150   }
151
152   public int compareTo(Object JavaDoc other) {
153     return compareTo((SynchronizedFloat)other);
154   }
155
156   public boolean equals(Object JavaDoc other) {
157     if (other != null &&
158         other instanceof SynchronizedFloat)
159       return get() == ((SynchronizedFloat)other).get();
160     else
161       return false;
162   }
163
164   public int hashCode() {
165     return Float.floatToIntBits(get());
166   }
167
168   public String JavaDoc toString() { return String.valueOf(get()); }
169
170 }
171
172
Popular Tags