package bonus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicVsNormal {
    private static int counter = 0;
    private static AtomicInteger atomicCounter = new AtomicInteger(0);

    public static void main(String[] args){
        int incrementCount = 1000;
        List<Thread> workers = new ArrayList<>();

        for (int i = 0; i < 10; i++){
            workers.add(new Thread(() ->{
                for (int j = 0; j < incrementCount; j++) {
                    counter++;
                    atomicCounter.incrementAndGet();
                }
            }
        ));
        }

        for (Thread worker : workers){
            worker.start();
        }

        for (Thread worker : workers){
            try {
                worker.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("Expected: 10000");
        System.out.println("Normal increment: " + counter);
        System.out.println("Atomic increment: " + atomicCounter.get());

    }
}
