import java.util.concurrent.atomic.AtomicInteger;
public class AlternatePrintingNumbersAtomic {
private static AtomicInteger count = new AtomicInteger(1);
private static final int MAX_COUNT = 10;
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
while (count.get() <= MAX_COUNT) {
if (count.get() % 2 == 1) {
System.out.println(Thread.currentThread().getName() + ": " + count.getAndIncrement());
}
}
}, "Thread1");
Thread thread2 = new Thread(() -> {
while (count.get() <= MAX_COUNT) {
if (count.get() % 2 == 0) {
System.out.println(Thread.currentThread().getName() + ": " + count.getAndIncrement());
}
}
}, "Thread2");
thread1.start();
thread2.start();
}
}