Commit 076d08e2 authored by Trisha Gee's avatar Trisha Gee
Browse files

[Ch15] Updated the synchronized version of Ryan and Monica so it actually works

parent 73f1d6ff
Loading
Loading
Loading
Loading
+21 −9
Changes for src/main/java/ch15/RyanAndMonicaSynchronizedTest.java: 21 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -2,10 +2,11 @@ package ch15;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class RyanAndMonicaSynchronizedTest {
  public static void main(String[] args) {
    BankAccount account = new BankAccount();
  public static void main(String[] args) throws InterruptedException {
    BankAccountSynchronized account = new BankAccountSynchronized();
    RyanAndMonicaSynchronizedJob ryan = new RyanAndMonicaSynchronizedJob("Ryan", account);
    RyanAndMonicaSynchronizedJob monica = new RyanAndMonicaSynchronizedJob("Monica", account);
    ExecutorService executor = Executors.newFixedThreadPool(2);
@@ -17,35 +18,46 @@ public class RyanAndMonicaSynchronizedTest {

class RyanAndMonicaSynchronizedJob implements Runnable {
  private final String name;
  private final BankAccount account;
  private final BankAccountSynchronized account;

  RyanAndMonicaSynchronizedJob(String name, BankAccount account) {
  RyanAndMonicaSynchronizedJob(String name, BankAccountSynchronized account) {
    this.name = name;
    this.account = account;
  }

  public void run() {
    for (int i = 0; i < 10; i++) {
      makeWithdrawal(10);
      account.makeWithdrawal(10, name);
      if (account.getBalance() < 0) {
        System.out.println("Overdrawn!");
      }
    }
  }
}

class BankAccountSynchronized {
  private int balance = 100;

  public int getBalance() {
    return balance;
  }

  private synchronized void makeWithdrawal(int amount) {
    if (account.getBalance() >= amount) {
  private void withdraw(int amount) {
    balance = balance - amount;
  }

  public synchronized void makeWithdrawal(int amount, String name) {
    if (balance >= amount) {
      System.out.println(name + " is about to withdraw");
      try {
        System.out.println(name + " is going to sleep");
        Thread.sleep(500);
      } catch (InterruptedException ex) {ex.printStackTrace();}
      System.out.println(name + " woke up.");
      account.withdraw(amount);
      withdraw(amount);
      System.out.println(name + " completes the withdrawal");
    } else {
      System.out.println("Sorry, not enough for " + name);
    }
  }
}
 No newline at end of file
+1 −6
Changes for src/main/java/ch15/RyanAndMonicaTest.java: 1 added line, 6 removed lines.
Original line number Diff line number Diff line
@@ -36,15 +36,10 @@ class RyanAndMonicaJob implements Runnable {

class BankAccount {
  private int balance = 100;

  public int getBalance() {
    return balance;
  }

  private void withdraw(int amount) {
    balance = balance - amount;
  }

  public void makeWithdrawal(int amount, String name) {
    if (balance >= amount) {
      System.out.println(name + " is about to withdraw");
@@ -53,7 +48,7 @@ class BankAccount {
        Thread.sleep(500);
      } catch (InterruptedException ex) {ex.printStackTrace();}
      System.out.println(name + " woke up.");
      withdraw(amount);
      balance = balance - amount;
      System.out.println(name + " completes the withdrawal");
    } else {
      System.out.println("Sorry, not enough for " + name);