Package edu.rit.pj.reduction
Class SharedFloat
java.lang.Object
java.lang.Number
edu.rit.pj.reduction.SharedFloat
- All Implemented Interfaces:
Serializable
Class SharedFloat provides a reduction variable for a value of type
float.
Class SharedFloat is multiple thread safe. The methods use lock-free atomic compare-and-set.
Note: Class SharedFloat is implemented using class
java.util.concurrent.atomic.AtomicInteger. The float value is stored as an
int whose bit pattern is the same as the float value.
- Version:
- 07-Jun-2007
- Author:
- Alan Kaminsky
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new float reduction variable with the initial value 0.SharedFloat(float initialValue) Construct a new float reduction variable with the given initial value. -
Method Summary
Modifier and TypeMethodDescriptionfloataddAndGet(float value) Add the given value to this reduction variable and return the new value.booleancompareAndSet(float expect, float update) Atomically set this reduction variable to the given updated value if the current value equals the expected value.floatSubtract one from this reduction variable and return the new value.doubleReturns this reduction variable's current value converted to typedouble.floatReturns this reduction variable's current value converted to typefloat.floatget()Returns this reduction variable's current value.floatgetAndAdd(float value) Add the given value to this reduction variable and return the previous value.floatSubtract one from this reduction variable and return the previous value.floatAdd one to this reduction variable and return the previous value.floatgetAndSet(float value) Set this reduction variable to the given value and return the previous value.floatAdd one to this reduction variable and return the new value.intintValue()Returns this reduction variable's current value converted to typeint.longReturns this reduction variable's current value converted to typelong.floatCombine this reduction variable with the given value using the given operation.voidset(float value) Set this reduction variable to the given value.toString()Returns a string version of this reduction variable.booleanweakCompareAndSet(float expect, float update) Atomically set this reduction variable to the given updated value if the current value equals the expected value.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Constructor Details
-
SharedFloat
public SharedFloat()Construct a new float reduction variable with the initial value 0. -
SharedFloat
public SharedFloat(float initialValue) Construct a new float reduction variable with the given initial value.- Parameters:
initialValue- Initial value.
-
-
Method Details
-
get
public float get()Returns this reduction variable's current value.- Returns:
- Current value.
-
set
public void set(float value) Set this reduction variable to the given value.- Parameters:
value- New value.
-
getAndSet
public float getAndSet(float value) Set this reduction variable to the given value and return the previous value.- Parameters:
value- New value.- Returns:
- Previous value.
-
compareAndSet
public boolean compareAndSet(float expect, float update) Atomically set this reduction variable to the given updated value if the current value equals the expected value.- Parameters:
expect- Expected value.update- Updated value.- Returns:
- True if the update happened, false otherwise.
-
weakCompareAndSet
public boolean weakCompareAndSet(float expect, float update) Atomically set this reduction variable to the given updated value if the current value equals the expected value. May fail spuriously.- Parameters:
expect- Expected value.update- Updated value.- Returns:
- True if the update happened, false otherwise.
-
getAndIncrement
public float getAndIncrement()Add one to this reduction variable and return the previous value.- Returns:
- Previous value.
-
getAndDecrement
public float getAndDecrement()Subtract one from this reduction variable and return the previous value.- Returns:
- Previous value.
-
getAndAdd
public float getAndAdd(float value) Add the given value to this reduction variable and return the previous value.- Parameters:
value- Value to add.- Returns:
- Previous value.
-
incrementAndGet
public float incrementAndGet()Add one to this reduction variable and return the new value.- Returns:
- New value.
-
decrementAndGet
public float decrementAndGet()Subtract one from this reduction variable and return the new value.- Returns:
- New value.
-
addAndGet
public float addAndGet(float value) Add the given value to this reduction variable and return the new value.- Parameters:
value- Value to add.- Returns:
- New value.
-
reduce
Combine this reduction variable with the given value using the given operation. The result is stored back into this reduction variable and is returned.- Parameters:
value- Value.op- Binary operation.- Returns:
- (This variable) op (
value).
-
toString
Returns a string version of this reduction variable. -
intValue
public int intValue()Returns this reduction variable's current value converted to typeint. -
longValue
public long longValue()Returns this reduction variable's current value converted to typelong. -
floatValue
public float floatValue()Returns this reduction variable's current value converted to typefloat.- Specified by:
floatValuein classNumber- Returns:
- Current value.
-
doubleValue
public double doubleValue()Returns this reduction variable's current value converted to typedouble.- Specified by:
doubleValuein classNumber- Returns:
- Current value.
-