001/* 002 * Written by Doug Lea and Martin Buchholz with assistance from 003 * members of JCP JSR-166 Expert Group and released to the public 004 * domain, as explained at 005 * http://creativecommons.org/publicdomain/zero/1.0/ 006 */ 007 008/* 009 * Source: 010 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/extra/AtomicDouble.java?revision=1.13 011 * (Modified to adapt to guava coding conventions and 012 * to use AtomicLong instead of sun.misc.Unsafe) 013 */ 014 015package com.google.common.util.concurrent; 016 017import static java.lang.Double.doubleToRawLongBits; 018import static java.lang.Double.longBitsToDouble; 019 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.concurrent.atomic.AtomicLong; 022 023/** 024 * A {@code double} value that may be updated atomically. See the {@link 025 * java.util.concurrent.atomic} package specification for description of the properties of atomic 026 * variables. An {@code AtomicDouble} is used in applications such as atomic accumulation, and 027 * cannot be used as a replacement for a {@link Double}. However, this class does extend {@code 028 * Number} to allow uniform access by tools and utilities that deal with numerically-based classes. 029 * 030 * <p><a id="bitEquals"></a>This class compares primitive {@code double} values in methods such as 031 * {@link #compareAndSet} by comparing their bitwise representation using {@link 032 * Double#doubleToRawLongBits}, which differs from both the primitive double {@code ==} operator and 033 * from {@link Double#equals}, as if implemented by: 034 * 035 * <pre>{@code 036 * static boolean bitEquals(double x, double y) { 037 * long xBits = Double.doubleToRawLongBits(x); 038 * long yBits = Double.doubleToRawLongBits(y); 039 * return xBits == yBits; 040 * } 041 * }</pre> 042 * 043 * <p>It is possible to write a more scalable updater, at the cost of giving up strict atomicity. 044 * See for example <a 045 * href="http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java.base/java/util/concurrent/atomic/DoubleAdder.html"> 046 * DoubleAdder</a>. 047 * 048 * @author Doug Lea 049 * @author Martin Buchholz 050 * @since 11.0 051 */ 052@ElementTypesAreNonnullByDefault 053public class AtomicDouble extends Number implements java.io.Serializable { 054 private static final long serialVersionUID = 0L; 055 056 // We would use AtomicLongFieldUpdater, but it has issues on some Android devices. 057 private transient AtomicLong value; 058 059 /** 060 * Creates a new {@code AtomicDouble} with the given initial value. 061 * 062 * @param initialValue the initial value 063 */ 064 public AtomicDouble(double initialValue) { 065 value = new AtomicLong(doubleToRawLongBits(initialValue)); 066 } 067 068 /** Creates a new {@code AtomicDouble} with initial value {@code 0.0}. */ 069 public AtomicDouble() { 070 this(0.0); 071 } 072 073 /** 074 * Gets the current value. 075 * 076 * @return the current value 077 */ 078 public final double get() { 079 return longBitsToDouble(value.get()); 080 } 081 082 /** 083 * Sets to the given value. 084 * 085 * @param newValue the new value 086 */ 087 public final void set(double newValue) { 088 long next = doubleToRawLongBits(newValue); 089 value.set(next); 090 } 091 092 /** 093 * Eventually sets to the given value. 094 * 095 * @param newValue the new value 096 */ 097 public final void lazySet(double newValue) { 098 long next = doubleToRawLongBits(newValue); 099 value.lazySet(next); 100 } 101 102 /** 103 * Atomically sets to the given value and returns the old value. 104 * 105 * @param newValue the new value 106 * @return the previous value 107 */ 108 public final double getAndSet(double newValue) { 109 long next = doubleToRawLongBits(newValue); 110 return longBitsToDouble(value.getAndSet(next)); 111 } 112 113 /** 114 * Atomically sets the value to the given updated value if the current value is <a 115 * href="#bitEquals">bitwise equal</a> to the expected value. 116 * 117 * @param expect the expected value 118 * @param update the new value 119 * @return {@code true} if successful. False return indicates that the actual value was not 120 * bitwise equal to the expected value. 121 */ 122 public final boolean compareAndSet(double expect, double update) { 123 return value.compareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 124 } 125 126 /** 127 * Atomically sets the value to the given updated value if the current value is <a 128 * href="#bitEquals">bitwise equal</a> to the expected value. 129 * 130 * <p>May <a 131 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 132 * fail spuriously</a> and does not provide ordering guarantees, so is only rarely an appropriate 133 * alternative to {@code compareAndSet}. 134 * 135 * @param expect the expected value 136 * @param update the new value 137 * @return {@code true} if successful 138 */ 139 public final boolean weakCompareAndSet(double expect, double update) { 140 return value.weakCompareAndSet(doubleToRawLongBits(expect), doubleToRawLongBits(update)); 141 } 142 143 /** 144 * Atomically adds the given value to the current value. 145 * 146 * @param delta the value to add 147 * @return the previous value 148 */ 149 @CanIgnoreReturnValue 150 public final double getAndAdd(double delta) { 151 while (true) { 152 long current = value.get(); 153 double currentVal = longBitsToDouble(current); 154 double nextVal = currentVal + delta; 155 long next = doubleToRawLongBits(nextVal); 156 if (value.compareAndSet(current, next)) { 157 return currentVal; 158 } 159 } 160 } 161 162 /** 163 * Atomically adds the given value to the current value. 164 * 165 * @param delta the value to add 166 * @return the updated value 167 */ 168 @CanIgnoreReturnValue 169 public final double addAndGet(double delta) { 170 while (true) { 171 long current = value.get(); 172 double currentVal = longBitsToDouble(current); 173 double nextVal = currentVal + delta; 174 long next = doubleToRawLongBits(nextVal); 175 if (value.compareAndSet(current, next)) { 176 return nextVal; 177 } 178 } 179 } 180 181 /** 182 * Returns the String representation of the current value. 183 * 184 * @return the String representation of the current value 185 */ 186 @Override 187 public String toString() { 188 return Double.toString(get()); 189 } 190 191 /** 192 * Returns the value of this {@code AtomicDouble} as an {@code int} after a narrowing primitive 193 * conversion. 194 */ 195 @Override 196 public int intValue() { 197 return (int) get(); 198 } 199 200 /** 201 * Returns the value of this {@code AtomicDouble} as a {@code long} after a narrowing primitive 202 * conversion. 203 */ 204 @Override 205 public long longValue() { 206 return (long) get(); 207 } 208 209 /** 210 * Returns the value of this {@code AtomicDouble} as a {@code float} after a narrowing primitive 211 * conversion. 212 */ 213 @Override 214 public float floatValue() { 215 return (float) get(); 216 } 217 218 /** Returns the value of this {@code AtomicDouble} as a {@code double}. */ 219 @Override 220 public double doubleValue() { 221 return get(); 222 } 223 224 /** 225 * Saves the state to a stream (that is, serializes it). 226 * 227 * @serialData The current value is emitted (a {@code double}). 228 */ 229 private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { 230 s.defaultWriteObject(); 231 232 s.writeDouble(get()); 233 } 234 235 /** Reconstitutes the instance from a stream (that is, deserializes it). */ 236 private void readObject(java.io.ObjectInputStream s) 237 throws java.io.IOException, ClassNotFoundException { 238 s.defaultReadObject(); 239 value = new AtomicLong(); 240 set(s.readDouble()); 241 } 242}