import java.util.concurrent.atomic.AtomicLong; public class Main { public static void main(String[] args) { AtomicLong atomicLong = new AtomicLong(10); long value = atomicLong.get(); System.out.println("The value of atomic long is: " + value); } }
import java.util.concurrent.atomic.AtomicLong; public class Main { public static AtomicLong counter = new AtomicLong(); public static void main(String[] args) { incrementCounter(); incrementCounter(); incrementCounter(); System.out.println("The value of counter is: " + counter.get()); } public static void incrementCounter() { counter.incrementAndGet(); } }In this example, we create a static AtomicLong variable called counter and initialize it with a value of 0. We write a method called incrementCounter() which increments this counter by one using the incrementAndGet() method. We call this method three times and print the final value of the counter to the console. The package library for the java.util.concurrent.atomic package is java.util.concurrent.