Package org.logicalcobwebs.concurrent
Class SynchronizedInt
- java.lang.Object
-
- org.logicalcobwebs.concurrent.SynchronizedVariable
-
- org.logicalcobwebs.concurrent.SynchronizedInt
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.lang.Comparable
,Executor
public class SynchronizedInt extends SynchronizedVariable implements java.lang.Comparable, java.lang.Cloneable
A class useful for offloading synch for int instance variables.
-
-
Field Summary
Fields Modifier and Type Field Description protected int
value_
-
Fields inherited from class org.logicalcobwebs.concurrent.SynchronizedVariable
lock_
-
-
Constructor Summary
Constructors Constructor Description SynchronizedInt(int initialValue)
Make a new SynchronizedInt with the given initial value, and using its own internal lock.SynchronizedInt(int initialValue, java.lang.Object lock)
Make a new SynchronizedInt with the given initial value, and using the supplied lock.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
add(int amount)
Add amount to value (i.e., set value += amount)int
and(int b)
Set value to value & b.boolean
commit(int assumedValue, int newValue)
Set value to newValue only if it is currently assumedValue.int
compareTo(int other)
int
compareTo(java.lang.Object other)
int
compareTo(SynchronizedInt other)
int
complement()
Set the value to its complementint
decrement()
Decrement the value.int
divide(int factor)
Divide value by factor (i.e., set value /= factor)boolean
equals(java.lang.Object other)
int
get()
Return the current valueint
hashCode()
int
increment()
Increment the value.int
multiply(int factor)
Multiply value by factor (i.e., set value *= factor)int
negate()
Set the value to the negative of its old valueint
or(int b)
Set value to value | b.int
set(int newValue)
Set to newValue.int
subtract(int amount)
Subtract amount from value (i.e., set value -= amount)int
swap(SynchronizedInt other)
Atomically swap values with another SynchronizedInt.java.lang.String
toString()
int
xor(int b)
Set value to value ^ b.-
Methods inherited from class org.logicalcobwebs.concurrent.SynchronizedVariable
execute, getLock
-
-
-
-
Constructor Detail
-
SynchronizedInt
public SynchronizedInt(int initialValue)
Make a new SynchronizedInt with the given initial value, and using its own internal lock.
-
SynchronizedInt
public SynchronizedInt(int initialValue, java.lang.Object lock)
Make a new SynchronizedInt with the given initial value, and using the supplied lock.
-
-
Method Detail
-
get
public final int get()
Return the current value
-
set
public int set(int newValue)
Set to newValue.- Returns:
- the old value
-
commit
public boolean commit(int assumedValue, int newValue)
Set value to newValue only if it is currently assumedValue.- Returns:
- true if successful
-
swap
public int swap(SynchronizedInt other)
Atomically swap values with another SynchronizedInt. Uses identityHashCode to avoid deadlock when two SynchronizedInts attempt to simultaneously swap with each other. (Note: Ordering via identyHashCode is not strictly guaranteed by the language specification to return unique, orderable values, but in practice JVMs rely on them being unique.)- Returns:
- the new value
-
increment
public int increment()
Increment the value.- Returns:
- the new value
-
decrement
public int decrement()
Decrement the value.- Returns:
- the new value
-
add
public int add(int amount)
Add amount to value (i.e., set value += amount)- Returns:
- the new value
-
subtract
public int subtract(int amount)
Subtract amount from value (i.e., set value -= amount)- Returns:
- the new value
-
multiply
public int multiply(int factor)
Multiply value by factor (i.e., set value *= factor)- Returns:
- the new value
-
divide
public int divide(int factor)
Divide value by factor (i.e., set value /= factor)- Returns:
- the new value
-
negate
public int negate()
Set the value to the negative of its old value- Returns:
- the new value
-
complement
public int complement()
Set the value to its complement- Returns:
- the new value
-
and
public int and(int b)
Set value to value & b.- Returns:
- the new value
-
or
public int or(int b)
Set value to value | b.- Returns:
- the new value
-
xor
public int xor(int b)
Set value to value ^ b.- Returns:
- the new value
-
compareTo
public int compareTo(int other)
-
compareTo
public int compareTo(SynchronizedInt other)
-
compareTo
public int compareTo(java.lang.Object other)
- Specified by:
compareTo
in interfacejava.lang.Comparable
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-