01: /**
02:    A transfer thread repeatedly transfers money between three
03:    bank accounts.
04: */
05: class TransferThread extends Thread
06: {
07:    /**
08:       Constructs a transfer thread.
09:       @param account1 the first account from which to withdraw
10:       @param account2 the second account from which to withdraw
11:       @param account3 the account to which to deposit
12:       @param anAmount the amount to withdraw from each of the 
13:       first two accounts.
14:    */
15:    public TransferThread(BankAccount account1,
16:       BankAccount account2, BankAccount account3,
17:       double anAmount)
18:    {
19:       from1 = account1;
20:       from2 = account2;
21:       to = account3;
22:       amount = anAmount;
23:    }
24: 
25:    public void run()
26:    {
27:       try
28:       {
29:          for (int i = 1; i <= REPETITIONS && !isInterrupted(); i++)
30:          {
31:             from1.withdraw(amount);
32:             from2.withdraw(amount);
33:             to.deposit(2 * amount);
34:             sleep(DELAY);         
35:          }
36:       }
37:       catch (InterruptedException exception)
38:       {
39:       }
40:    }
41: 
42:    private BankAccount from1;
43:    private BankAccount from2;
44:    private BankAccount to;
45:    private double amount;
46: 
47:    private static final int REPETITIONS = 10;
48:    private static final int DELAY = 10;
49: }
50: