Classes java.util.concurrent.atomic provides to atomic access for Boolean, Integer, Long, Reference, their array and reflection-based classes (AtomicXXXFieldUpdater, it is used to enable atomic updates to
designated volatile XXX fields of designated classes).
public Task(Container c) { this.c = c; } @Override public void run() { System.out.println(updater.getAndIncrement(c)); System.out.println(updater.getAndIncrement(c)); } }
public class UpdaterUsage { public static void main (String [] args) { Container c = new Container(); Task t1 = new Task(c); Task t2 = new Task(c); t1.start(); t2.start(); } }