Method from EDU.oswego.cs.dl.util.concurrent.SynchronizedChar Detail: |
public char add(char amount) {
synchronized (lock_) {
return value_ += amount;
}
}
Add amount to value (i.e., set value += amount) |
public boolean commit(char assumedValue,
char newValue) {
synchronized(lock_) {
boolean success = (assumedValue == value_);
if (success) value_ = newValue;
return success;
}
}
Set value to newValue only if it is currently assumedValue. |
public int compareTo(char other) {
char val = get();
return (val < other)? -1 : (val == other)? 0 : 1;
}
|
public int compareTo(SynchronizedChar other) {
return compareTo(other.get());
}
|
public int compareTo(Object other) {
return compareTo((SynchronizedChar)other);
}
|
public char divide(char factor) {
synchronized (lock_) {
return value_ /= factor;
}
}
Divide value by factor (i.e., set value /= factor) |
public boolean equals(Object other) {
if (other != null &&
other instanceof SynchronizedChar)
return get() == ((SynchronizedChar)other).get();
else
return false;
}
|
public final char get() {
synchronized(lock_) { return value_; }
}
|
public int hashCode() {
// same hash as Char
return (int)(get());
}
|
public synchronized char multiply(char factor) {
synchronized (lock_) {
return value_ *= factor;
}
}
Multiply value by factor (i.e., set value *= factor) |
public char set(char newValue) {
synchronized (lock_) {
char old = value_;
value_ = newValue;
return old;
}
}
|
public char subtract(char amount) {
synchronized (lock_) {
return value_ -= amount;
}
}
Subtract amount from value (i.e., set value -= amount) |
public char swap(SynchronizedChar other) {
if (other == this) return get();
SynchronizedChar fst = this;
SynchronizedChar snd = other;
if (System.identityHashCode(fst) > System.identityHashCode(snd)) {
fst = other;
snd = this;
}
synchronized(fst.lock_) {
synchronized(snd.lock_) {
fst.set(snd.set(fst.get()));
return get();
}
}
}
Atomically swap values with another SynchronizedChar.
Uses identityHashCode to avoid deadlock when
two SynchronizedChars 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.) |
public String toString() {
return String.valueOf(get());
}
|