예제 #1
0
  public void run() {
    synchronized (receiver) {
      synchronized (giver) {
        int stopCondition = 0;
        Log.i("bank", name + " is $" + receiver.getBalance() + ".");
        // Take money
        while (stopCondition < maxWithdraw) {
          // Withdraw
          int withdrawNum = giver.withdraw(ammountPerWithdraw);
          // Deposite
          receiver.deposite(withdrawNum);
          // Stop counter
          stopCondition += withdrawNum;

          // Sleep for a bit
          try {
            Thread.sleep(sleepDelay);
          } catch (InterruptedException e) {
          }

          // Display GUI changes
          activity
              .getHandler()
              .post(
                  new Runnable() {
                    public void run() {
                      activity.getField1().setText("" + giver.getBalance());
                      activity.getField2().setText("" + receiver.getBalance());
                    }
                  });
        }

        Log.i("bank", name + "has stopped receiving.");
      }
    }
  }