// Atomically adds the given value to the current value of the field of the given object managed by this updater. publicintaddAndGet(T obj, int delta){
// Atomically sets the field of the given object managed by this updater to the given updated value if the current value {@code ==} the expected value. publicabstractbooleancompareAndSet(T obj, int expect, int update);
// Atomically decrements by one the current value of the field of the given object managed by this updater. publicintdecrementAndGet(T obj) // Returns the current value held in the field of the given object managed by this updater. publicabstractintget(T obj);
// Atomically sets the field of the given object managed by this updater to the given value and returns the old value. publicintgetAndSet(T obj, int newValue) // Atomically increments by one the current value of the field of the given object managed by this updater. publicintincrementAndGet(T obj) // Eventually sets the field of the given object managed by this updater to the given updated value. publicabstractvoidlazySet(T obj, int newValue) // Creates and returns an updater for objects with the given field. publicstatic <U> AtomicIntegerFieldUpdater<U> newUpdater(Class<U> tclass, String fieldName) // Sets the field of the given object managed by this updater to the given updated value publicabstractvoidset(T obj, int newValue)
publicclassIntFieldTest{ publicstaticvoidmain(String[] args){ Class cls = Person.class; // Create AtomicIntFieldUpdater object, parameter is "class object" and "int variable's name" AtomicIntFieldUpdater mAtoInt = AtomicIntFieldUpdater.newUpdater(cls, "id"); Person person = new Person(12345);
// Compare person's id. If it's 12345, set to 1000 mAtoInt.compareAndSet(person, 12345, 1000); System.out.println("id = " + person.getId()); } }