package dev.banking.bonus;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicCounterDemo
{

    private static int normalCounter = 0;
    private static final AtomicInteger atomicCounter = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException
    {
        int threadCount = 10;
        int incrementsPerThread = 100_000;

        Thread[] threads = new Thread[threadCount];

        for (int i = 0; i < threadCount; i++)
        {
            threads[i] = new Thread(() -> {
                for (int j = 0; j < incrementsPerThread; j++)
                {
                    normalCounter++;
                    atomicCounter.incrementAndGet();
                }
            });
        }

        for (Thread thread : threads)
        {
            thread.start();
        }

        for (Thread thread : threads)
        {
            thread.join();
        }

        int expected = threadCount * incrementsPerThread;

        System.out.println("Expected value: " + expected);
        System.out.println("Normal int value: " + normalCounter);
        System.out.println("AtomicInteger value: " + atomicCounter.get());
    }
}