public class Test42 { public static void main(String[] args) { Account.demo(new MyAtomicInteger(10000)); }}class MyAtomicInteger implements Account { private volatile int value; private static final long valueOffset; private static final Unsafe UNSAFE; static { UNSAFE = UnsafeAccessor.getUnsafe(); try { valueOffset = UNSAFE.objectFieldOffset(MyAtomicInteger.class.getDeclaredField("value")); } catch (NoSuchFieldException e) { e.printStackTrace(); throw new RuntimeException(e); } } public int getValue() { return value; } public void decrement(int amount) { while (true) { int prev = this.value; int next = prev - amount; if (UNSAFE.compareAndSwapInt(this, valueOffset, prev, next)) { break; } } } public MyAtomicInteger(int value) { this.value = value; } @Override public Integer getBalance() { return getValue(); } @Override public void withdraw(Integer amount) { decrement(amount); }}
public class UnsafeAccessor { private static final Unsafe unsafe; static { try { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); unsafe = (Unsafe) theUnsafe.get(null); } catch (NoSuchFieldException | IllegalAccessException e) { throw new Error(e); } } public static Unsafe getUnsafe() { return unsafe; }}