import java.util.concurrent.Semaphore; /** * An integer that can be incremented atomically. * * @author Franck van Breugel */ public class AtomicInteger { private Semaphore semaphore; private int value; /** * Initializes this atomic integer with the given value. * * @param value the initial value of this atomic integer. */ public AtomicInteger(int value) { super(); this.semaphore = new Semaphore(1); this.value = value; } /** * Increments atomically. */ public void increment() { try { this.semaphore.acquire(); this.value++; this.semaphore.release(); } catch (InterruptedException e) { // do nothing } } }