package atomic.reference;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.ThreadLocalRandom.current;

public class AtomicExample03 {
    static volatile DebitCard debitCard = new DebitCard("Bunny", 0);

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread("B-" + i) {
                @Override
                public void run() {
                    while (true) {
                        final DebitCard dc = debitCard;
                        DebitCard newDc = new DebitCard(dc.getAccount(), debitCard.getAmount() + 10);
                        System.out.println(newDc);

                        debitCard = newDc;
                        try {
                            TimeUnit.MILLISECONDS.sleep(current().nextInt(20));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
    }
}
