import java.util.concurrent.atomic.AtomicInteger; public class AtomicVariableExample { // Atomic integer variable private final AtomicInteger count = new AtomicInteger(0); public void increment() { // Atomically increments by one the current value count.incrementAndGet(); } public void decrement() { // Atomically decrements by one the current value count.decrementAndGet(); } public int getCount() { // Returns the current value return count.get(); } public static void main(String[] args) throws InterruptedException { AtomicVariableExample example = new AtomicVariableExample(); // Thread for incrementing the count Thread incrementThread = new Thread(() -> { for (int i = 0; i < 1000; i++) { example.increment(); } }); // Thread for decrementing the count Thread decrementThread = new Thread(() -> { for (int i = 0; i < 1000; i++) { example.decrement(); } }); incrementThread.start(); decrementThread.start(); // Wait for both threads to finish incrementThread.join(); decrementThread.join(); // Print the final count value System.out.println("Final count value: " + example.getCount()); } }