Package edu.rit.pj.reduction
Class SharedShort
java.lang.Object
java.lang.Number
edu.rit.pj.reduction.SharedShort
- All Implemented Interfaces:
Serializable
Class SharedShort provides a reduction variable for a value of type
short
.
Class SharedShort is multiple thread safe. The methods use lock-free atomic compare-and-set.
Note: Class SharedShort is implemented using class
java.util.concurrent.atomic.AtomicInteger. The short value is stored as an
int
whose values are restricted to the range of type short
.
- Version:
- 07-Jun-2007
- Author:
- Alan Kaminsky
- See Also:
-
Constructor Summary
ConstructorDescriptionConstruct a new short reduction variable with the initial value 0.SharedShort
(short initialValue) Construct a new short reduction variable with the given initial value. -
Method Summary
Modifier and TypeMethodDescriptionshort
addAndGet
(short value) Add the given value to this reduction variable and return the new value.boolean
compareAndSet
(short expect, short update) Atomically set this reduction variable to the given updated value if the current value equals the expected value.short
Subtract one from this reduction variable and return the new value.double
Returns this reduction variable's current value converted to typedouble
.float
Returns this reduction variable's current value converted to typefloat
.short
get()
Returns this reduction variable's current value.short
getAndAdd
(short value) Add the given value to this reduction variable and return the previous value.short
Subtract one from this reduction variable and return the previous value.short
Add one to this reduction variable and return the previous value.short
getAndSet
(short value) Set this reduction variable to the given value and return the previous value.short
Add one to this reduction variable and return the new value.int
intValue()
Returns this reduction variable's current value converted to typeint
.long
Returns this reduction variable's current value converted to typelong
.short
Combine this reduction variable with the given value using the given operation.void
set
(short value) Set this reduction variable to the given value.toString()
Returns a string version of this reduction variable.boolean
weakCompareAndSet
(short expect, short 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
-
SharedShort
public SharedShort()Construct a new short reduction variable with the initial value 0. -
SharedShort
public SharedShort(short initialValue) Construct a new short reduction variable with the given initial value.- Parameters:
initialValue
- Initial value.
-
-
Method Details
-
get
public short get()Returns this reduction variable's current value.- Returns:
- Current value.
-
set
public void set(short value) Set this reduction variable to the given value.- Parameters:
value
- New value.
-
getAndSet
public short getAndSet(short 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(short expect, short 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(short expect, short 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 short getAndIncrement()Add one to this reduction variable and return the previous value.- Returns:
- Previous value.
-
getAndDecrement
public short getAndDecrement()Subtract one from this reduction variable and return the previous value.- Returns:
- Previous value.
-
getAndAdd
public short getAndAdd(short value) Add the given value to this reduction variable and return the previous value.- Parameters:
value
- Value to add.- Returns:
- Previous value.
-
incrementAndGet
public short incrementAndGet()Add one to this reduction variable and return the new value.- Returns:
- New value.
-
decrementAndGet
public short decrementAndGet()Subtract one from this reduction variable and return the new value.- Returns:
- New value.
-
addAndGet
public short addAndGet(short 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:
floatValue
in classNumber
- Returns:
- Current value.
-
doubleValue
public double doubleValue()Returns this reduction variable's current value converted to typedouble
.- Specified by:
doubleValue
in classNumber
- Returns:
- Current value.
-