import java.util.concurrent.atomic.AtomicInteger; public class AtomicCounter { private AtomicInteger counter = new AtomicInteger(0); public int getNext() { return counter.getAndSet(counter.get() + 1); } }
import java.util.concurrent.atomic.AtomicInteger; public class AtomicUpdater { private static AtomicInteger value = new AtomicInteger(0); public static void main(String[] args) { int previousValue = value.getAndSet(2); System.out.println("Previous value was " + previousValue + ", new value is " + value); } }In this example, a class called AtomicUpdater is created that updates a single AtomicInteger called value. The class sets the value to 2 using getAndSet(), and also prints out the previous value before the update. In summary, java.util.concurrent.atomic is a package library in Java that provides atomic variables for concurrent programming. The AtomicInteger class provides the getAndSet() method, which atomically sets the value of the AtomicInteger and returns the previous value.