// private synchronized boolean makeWithdrawal(int withdrawalAmount) {
  private synchronized void makeWithdrawal(int withdrawalAmount) {

    if (account.getAccountBalance() >= withdrawalAmount) {
      System.out.println("\n+ Account balance is sufficient enough to make a withdrawl...");
      System.out.println(Thread.currentThread().getName() + " is about to make an withdrawal.");

      Random random = new Random();
      int sleepTime = random.nextInt(500) + 510;
      System.out.println("+ Sleeping for - " + sleepTime);
      // Thread.sleep(sleepTime);

      System.out.println(
          "+ Wake-up Call - " + Thread.currentThread().getName() + " just WOKE UP...");

      TOTAL_WITHDRAWN.set(TOTAL_WITHDRAWN.get() + account.withdrawal(10));
      System.out.println(
          "+ Total Withdrawal by "
              + Thread.currentThread().getName()
              + " = "
              + TOTAL_WITHDRAWN.get());
      System.out.println("+ Account Balance = " + account.getAccountBalance() + "\n");

    } else {
      System.out.println(
          "+ WARNING: Sorry "
              + Thread.currentThread().getName()
              + ", insufficient account balance!!!");
    }
  }
コード例 #2
0
    public void startTransaction() {

      try {
        bankAdminLOCK.acquire();
        if (account.getAccountBalance() > 0) {
          System.out.println(
              "+++ " + Thread.currentThread().getName() + " - is the user in" + " transaction.");

          Random random = new Random();
          int sleepTime = random.nextInt(500) + 510;
          System.out.println("+ Zzzzz...... fallen asleep for - " + sleepTime + " secs.");
          Thread.sleep(sleepTime);

          System.out.println(
              "+ Wake-up Call - " + Thread.currentThread().getName() + " just WOKE UP...");

          TOTAL_WITHDRAWN.set(TOTAL_WITHDRAWN.get() + account.withdrawal(10));
          System.out.println(
              "+ Total Withdrawal by "
                  + Thread.currentThread().getName()
                  + " = "
                  + TOTAL_WITHDRAWN.get());
          System.out.println("+ Account Balance = " + account.getAccountBalance() + "\n");

          bankAdminLOCK.release();
          System.out.println(
              Thread.currentThread().getName() + "'s Transaction " + "is successful...\n");

        } else {
          System.out.println("+++ WARNING: Insufficient account balance....");
          System.exit(0);
        }

      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  @Override
  public void run() {
    TOTAL_WITHDRAWN.set(0);
    for (int i = 1; i <= 60; i++) {
      makeWithdrawal(10);
      if (account.getAccountBalance() < 0) {
        System.out.println("+ User Warning: Insufficient balance notice is sent...");
        System.exit(0);
      }
    }
    /*
    System.out.println("Withdraw amount = " + account.withdrawal(90) + " and " +
            "Account Balance = " + account.getAccountBalance());

    System.out.println("Withdraw amount = " + account.withdrawal(30) + " and " +
            "Account Balance = " + account.getAccountBalance());
            */
  }