public class AccountThread extends Thread {
    public AccountThread(String threadname, BankAccount from, BankAccount to) {
        super(threadname);
        this.From = from;
        this.To = to; 
    }
    
    BankAccount From;
    BankAccount To;

    public void run() {
        int from_amount = 0;
        int to_amount = 0;
        
        for (int i = 0; i < 10; i++) {
            // what if you change the monitor or comment it out completely
            // synchronized (this)
            // synchronized (ThreadsAndAccounts.class)
            {
                from_amount = From.Amount;
                sleep();

                to_amount = To.Amount;
                sleep();

                int delta = (int)(Math.random() * from_amount);
                System.out.println("... "+super.getName()+" "+from_amount+" -> "+delta+" -> "+to_amount);

                to_amount = to_amount + delta;
                To.Amount = to_amount;
                sleep();

                from_amount = from_amount - delta;
                From.Amount = from_amount;
                sleep();
            }
        }
        
        System.out.println("*** "+super.getName());
    }

    void sleep() {
        try {
            sleep((long)(Math.random() * 100));
        } catch (InterruptedException ex) { }
    }
}
