public class AccountConflict2 {
    private Account account = new Account();
    private final int NUM_THREADS = 100;
    private Thread thread[] = new Thread[NUM_THREADS];
    private int completed = 0;

    public static void main(String args[]) {
        AccountConflict2 test = new AccountConflict2();
        System.out.println("The balance is: " + test.account.getBalance());
    } 

    // Constructor does the work of creating and launching the threads
    public AccountConflict2() {
        // Create and launch 100 threads
        for (int i=0; i<100; i++) {
            thread[i] = new AddAPennyThread();
              thread[i].start();
        }

        // Wait for threads to finish.
        while (completed < NUM_THREADS );
    } 

    // Nested class for the threads - contains the run method
    class AddAPennyThread extends Thread {
        public void run() {
            account.deposit(1);
            completed += 1;
        } 
    } 

    // Nested class for the "resource".
    class Account {
        private int balance = 0; // current balance
       
           public int getBalance() {
            return balance;
        } 

        public void deposit(int amount) {
            int newBalance = balance + amount;
               System.out.println(newBalance);
            Thread.yield();

            balance = newBalance;
        } 
    } 
}


