FileDocCategorySizeDatePackage
Bank3.javaAPI DocExample2332Sun Feb 13 16:52:30 GMT 2000None

Bank

public class Bank extends Object

Fields Summary
public static final int
NO_ACCS
public static final int
INIT_BAL
private int
noTransacts
private int[]
account
Constructors Summary
public Bank()

   
    
     account = new int[ NO_ACCS ];
      for( int i = 0; i < account.length; i++ )
      {  account[i] = INIT_BAL;
      }
      noTransacts = 0;
      currentState();
   
Methods Summary
public voidcurrentState()

  int bal = 0;
      for( int i=0; i < account.length; i++ )
      {  bal += account[i];
      }
      System.out.println( "Transactions:\t" + noTransacts +
                          "\tTotal balance:\t" + bal );
   
public static voidmain(java.lang.String[] args)

  Bank theBank = new Bank();
      for( int i=0; i < NO_ACCS; i++ )
      {  new TransactionGenerator( theBank, i ).start();
      }
      new Switcher();
   
public synchronized voidtransfer(int amount, int from, int to)

  if( account[from] < amount )
      {  System.out.println( "Account: " + from + " has less than " + amount );
      }
      while( account[from] < amount )
      {  try
         {  wait();
         }
         catch( InterruptedException e ) {}
      }
      account[from] -= amount;
      account[to] += amount;
      noTransacts += 1;
      if( noTransacts%1000 == 0 )
      {  currentState();
      }
      notifyAll();