FileDocCategorySizeDatePackage
Bank3.javaAPI DocExample2135Mon Oct 16 19:44:08 BST 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();
      }
   
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;
/**/  Thread.yield();
      account[to] += amount;

      noTransacts += 1;
      if( noTransacts%100 == 0 )
      {  currentState();
      }
      notifyAll();