* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(boolean expect, boolean update) {
- /*int e = expect ? 1 : 0;
+ int e = expect ? 1 : 0;
int u = update ? 1 : 0;
- return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/
- System.out.println("Unimplemented AtomicBoolean.compareAndSet()");
- return false;
+ synchronized (this) {
+ if(e == value) {
+ value = u;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ //return unsafe.compareAndSwapInt(this, valueOffset, e, u);
}
/**
* @return true if successful.
*/
public boolean weakCompareAndSet(boolean expect, boolean update) {
- /*int e = expect ? 1 : 0;
+ int e = expect ? 1 : 0;
int u = update ? 1 : 0;
- return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/
- System.out.println("Unimplemented AtomicBoolean.weakCompareAndSet()");
- return false;
+ synchronized (this) {
+ if(e == value) {
+ value = u;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ //return unsafe.compareAndSwapInt(this, valueOffset, e, u);*/
}
/**
*/
public final void lazySet(boolean newValue) {
int v = newValue ? 1 : 0;
+ synchronized (this) {
+ value = v;
+ }
//unsafe.putOrderedInt(this, valueOffset, v);
- System.out.println("Unimplemented AtomicBoolean.lazySet()");
}
/**
* @since 1.6
*/
public final void lazySet(int newValue) {
+ synchronized (this) {
+ value = newValue;
+ }
//unsafe.putOrderedInt(this, valueOffset, newValue);
- System.out.println("Unimplemented AtomicInteger.lazySet()!");
}
/**
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(int expect, int update) {
- System.out.println("Unimplemented AtomicInteger.compareAndSet()!");
- return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
+ synchronized (this) {
+ if(expect == value) {
+ value = update;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
/**
* @return true if successful.
*/
public final boolean weakCompareAndSet(int expect, int update) {
- System.out.println("Unimplemented AtomicInteger.weakCompareAndSet()!");
- return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
+ synchronized (this) {
+ if(expect == value) {
+ value = update;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
/**
* @since 1.6
*/
public final void lazySet(Object/*V*/ newValue) {
+ synchronized (this) {
+ value = newValue;
+ }
//unsafe.putOrderedObject(this, valueOffset, newValue);
- System.out.println("Unimplemented AtomicReference.lazySet()!");
}
/**
* the actual value was not equal to the expected value.
*/
public final boolean compareAndSet(Object/*V*/ expect, Object/*V*/ update) {
+ synchronized (this) {
+ if(expect == value) {
+ value = update;
+ return true;
+ } else {
+ return false;
+ }
+ }
//return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
- System.out.println("Unimplemented AtomicReference.compareAndSet()!");
}
/**
* @return true if successful.
*/
public final boolean weakCompareAndSet(Object/*V*/ expect, Object/*V*/ update) {
- //return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
- System.out.println("Unimplemented AtomicReference.weakCompareAndSet()!");
+ synchronized (this) {
+ if(expect == value) {
+ value = update;
+ return true;
+ } else {
+ return false;
+ }
+ }
+ //return unsafe.compareAndSwapObject(this, valueOffset, expect, update);\
}
/**