import java.util.concurrent.atomic.AtomicInteger; public class AtomicExample { public static void main(String[] args) { AtomicInteger atomicInteger = new AtomicInteger(10); atomicInteger.set(20); System.out.println("Atomic Integer value: " + atomicInteger.get()); } }
import java.util.concurrent.atomic.AtomicInteger; public class AtomicExample { public static void main(String[] args) { AtomicInteger atomicInteger = new AtomicInteger(10); int oldValue = atomicInteger.getAndSet(20); System.out.println("Old value: " + oldValue); System.out.println("New value: " + atomicInteger.get()); } }In this example, we have used the getAndSet() method of the AtomicInteger class, which atomically sets the value to the new value and returns the old value. We then print both the old and new values to the console. The package library for AtomicInteger is java.util.concurrent.atomic.