From 8bf3f5d8a17b8d5f5eaad0f777a2e240591d0434 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 22 Oct 2018 13:06:21 -0400 Subject: [PATCH 1/8] --- src/edu/temple/cis/c3238/banksim/Account.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/edu/temple/cis/c3238/banksim/Account.java b/src/edu/temple/cis/c3238/banksim/Account.java index 4082223..f613601 100644 --- a/src/edu/temple/cis/c3238/banksim/Account.java +++ b/src/edu/temple/cis/c3238/banksim/Account.java @@ -45,3 +45,7 @@ public String toString() { return String.format("Account[%d] balance %d", id, balance); } } + +/** + * Jenni is the best + */ From f2772c8f0cc832a53ecfe878f0e380d6871f0ab9 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 22 Oct 2018 13:26:09 -0400 Subject: [PATCH 2/8] we're the best --- src/edu/temple/cis/c3238/banksim/Account.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/edu/temple/cis/c3238/banksim/Account.java b/src/edu/temple/cis/c3238/banksim/Account.java index f613601..939b582 100644 --- a/src/edu/temple/cis/c3238/banksim/Account.java +++ b/src/edu/temple/cis/c3238/banksim/Account.java @@ -48,4 +48,7 @@ public String toString() { /** * Jenni is the best + * Casey is the best + * Alex is the best + * Anika is the best */ From dd136a6e79ad92638eeca4380371320ed609701a Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 17:13:47 -0400 Subject: [PATCH 3/8] Task 2 --- src/edu/temple/cis/c3238/banksim/Account.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/edu/temple/cis/c3238/banksim/Account.java b/src/edu/temple/cis/c3238/banksim/Account.java index 939b582..7c28e36 100644 --- a/src/edu/temple/cis/c3238/banksim/Account.java +++ b/src/edu/temple/cis/c3238/banksim/Account.java @@ -17,14 +17,14 @@ public Account(Bank myBank, int id, int initialBalance) { balance = initialBalance; } - public int getBalance() { + public synchronized int getBalance() { return balance; } - public boolean withdraw(int amount) { + public synchronized boolean withdraw(int amount) { if (amount <= balance) { int currentBalance = balance; -// Thread.yield(); // Try to force collision + //Thread.yield(); // Try to force collision int newBalance = currentBalance - amount; balance = newBalance; return true; @@ -33,22 +33,17 @@ public boolean withdraw(int amount) { } } - public void deposit(int amount) { + public synchronized void deposit(int amount) { int currentBalance = balance; -// Thread.yield(); // Try to force collision + //Thread.yield(); // Try to force collision int newBalance = currentBalance + amount; balance = newBalance; + + //notifyAll(); } @Override public String toString() { return String.format("Account[%d] balance %d", id, balance); } -} - -/** - * Jenni is the best - * Casey is the best - * Alex is the best - * Anika is the best - */ +} \ No newline at end of file From 4161bada96720d4f596cb851dc8cf05ba50e7839 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 17:31:37 -0400 Subject: [PATCH 4/8] Task 3 (?) I put synchronized on the Account class methods and modified the test method in Bank.java with Reentrant locks. --- src/edu/temple/cis/c3238/banksim/Bank.java | 46 ++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/src/edu/temple/cis/c3238/banksim/Bank.java b/src/edu/temple/cis/c3238/banksim/Bank.java index e419ba8..dfa6fe7 100644 --- a/src/edu/temple/cis/c3238/banksim/Bank.java +++ b/src/edu/temple/cis/c3238/banksim/Bank.java @@ -1,5 +1,6 @@ package edu.temple.cis.c3238.banksim; +import java.util.concurrent.locks.ReentrantLock; /** * @author Cay Horstmann * @author Modified by Paul Wolfgang @@ -13,6 +14,7 @@ public class Bank { private long ntransacts = 0; private final int initialBalance; private final int numAccounts; + private final ReentrantLock rlock = new ReentrantLock(); public Bank(int numAccounts, int initialBalance) { this.initialBalance = initialBalance; @@ -25,20 +27,49 @@ public Bank(int numAccounts, int initialBalance) { } public void transfer(int from, int to, int amount) { -// accounts[from].waitForAvailableFunds(amount); + + rlock.lock(); if (accounts[from].withdraw(amount)) { accounts[to].deposit(amount); } - if (shouldTest()) test(); - } + rlock.unlock(); + + rlock.lock(); + try { + + if (accounts[from].withdraw(amount)) { + accounts[to].deposit(amount); + } + + } finally { + rlock.unlock(); + } + if (shouldTest()) { + test(); + } + +} public void test() { + int sum = 0; - for (Account account : accounts) { - System.out.printf("%s %s%n", + + rlock.lock(); + + try { + + for (Account account : accounts) { + System.out.printf("%s %s%n", Thread.currentThread().toString(), account.toString()); - sum += account.getBalance(); + sum += account.getBalance(); + } + + } + + finally { + rlock.unlock(); } + System.out.println(Thread.currentThread().toString() + " Sum: " + sum); if (sum != numAccounts * initialBalance) { @@ -51,6 +82,7 @@ public void test() { } } + public int size() { return accounts.length; } @@ -60,4 +92,4 @@ public boolean shouldTest() { return ++ntransacts % NTEST == 0; } -} +} \ No newline at end of file From d2994a83af9d01390d87b11dc1b45d6b070760be Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 20:58:21 -0400 Subject: [PATCH 5/8] Working Project; all Tasks accomplished. Comments included to help us demo --- src/edu/temple/cis/c3238/banksim/Account.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/edu/temple/cis/c3238/banksim/Account.java b/src/edu/temple/cis/c3238/banksim/Account.java index 7c28e36..2b87dd5 100644 --- a/src/edu/temple/cis/c3238/banksim/Account.java +++ b/src/edu/temple/cis/c3238/banksim/Account.java @@ -1,5 +1,8 @@ package edu.temple.cis.c3238.banksim; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + /** * @author Cay Horstmann * @author Modified by Paul Wolfgang @@ -10,17 +13,25 @@ public class Account { private volatile int balance; private final int id; private final Bank myBank; - + public Account(Bank myBank, int id, int initialBalance) { this.myBank = myBank; this.id = id; balance = initialBalance; } - public synchronized int getBalance() { + /* + * getBalance() does not need to be synchronized, despite balance being + * a shared variable, because getBalance() does not modify balance. + **/ + public int getBalance() { return balance; } + /* + * withdraw() method is synchronized via synchronized keyword in it's + * declaration + **/ public synchronized boolean withdraw(int amount) { if (amount <= balance) { int currentBalance = balance; @@ -33,13 +44,29 @@ public synchronized boolean withdraw(int amount) { } } + /* + * synchronized same as withdraw() + **/ public synchronized void deposit(int amount) { int currentBalance = balance; //Thread.yield(); // Try to force collision int newBalance = currentBalance + amount; balance = newBalance; - //notifyAll(); + notifyAll(); + } + + /* + * implementation of task 4 + * this method waits for amount <= balance + **/ + public synchronized void waitForSufficientFunds(int amount) { + + while (myBank.isOpen() && amount >= balance) { + try { + wait(); + } catch (InterruptedException ex) {/*ignore*/} + } } @Override From 2a1fcec60d6d027c663ddbd3702c1e647c343c42 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 20:59:38 -0400 Subject: [PATCH 6/8] Working Project; all Tasks accomplished. Comments included to help us demo --- src/edu/temple/cis/c3238/banksim/Bank.java | 46 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/edu/temple/cis/c3238/banksim/Bank.java b/src/edu/temple/cis/c3238/banksim/Bank.java index dfa6fe7..af4d22c 100644 --- a/src/edu/temple/cis/c3238/banksim/Bank.java +++ b/src/edu/temple/cis/c3238/banksim/Bank.java @@ -16,6 +16,8 @@ public class Bank { private final int numAccounts; private final ReentrantLock rlock = new ReentrantLock(); + private boolean open = true; + public Bank(int numAccounts, int initialBalance) { this.initialBalance = initialBalance; this.numAccounts = numAccounts; @@ -28,22 +30,24 @@ public Bank(int numAccounts, int initialBalance) { public void transfer(int from, int to, int amount) { - rlock.lock(); - if (accounts[from].withdraw(amount)) { - accounts[to].deposit(amount); + if (!open) { + return; } - rlock.unlock(); - + + /* waiting for amount <= balance via method call */ + accounts[from].waitForSufficientFunds(amount); + + /* ReentrantLock used to manipulate account balances */ rlock.lock(); try { - if (accounts[from].withdraw(amount)) { accounts[to].deposit(amount); } - - } finally { + } + finally { rlock.unlock(); } + if (shouldTest()) { test(); } @@ -54,6 +58,10 @@ public void test() { int sum = 0; + /* + * the shared part of this method is "account.getBalance" + * hence, we lock that part with a ReentrantLock + **/ rlock.lock(); try { @@ -91,5 +99,27 @@ public int size() { public boolean shouldTest() { return ++ntransacts % NTEST == 0; } + + /* returns true when the account is open, false otherwise */ + public synchronized boolean isOpen() { + return open; + } + + /* method to satisfy Task 5; + * closeBank() closes all accounts */ + public void closeBank() { + + /* the open variable is shared and hence protected */ + synchronized (this) { + open = false; + } + + /* wake up all waiting threads */ + for (Account account : accounts) { + synchronized (account) { + account.notifyAll(); + } + } + } } \ No newline at end of file From c4ed1906e2657ef8e121702f74a9ab3709e339b7 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 20:59:47 -0400 Subject: [PATCH 7/8] Working Project; all Tasks accomplished. Comments included to help us demo --- src/edu/temple/cis/c3238/banksim/BankSimMain.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/edu/temple/cis/c3238/banksim/BankSimMain.java b/src/edu/temple/cis/c3238/banksim/BankSimMain.java index 7357332..04d8f89 100644 --- a/src/edu/temple/cis/c3238/banksim/BankSimMain.java +++ b/src/edu/temple/cis/c3238/banksim/BankSimMain.java @@ -18,10 +18,6 @@ public static void main(String[] args) { threads[i] = new TransferThread(b, i, INITIAL_BALANCE); threads[i].start(); } - -// b.test(); System.out.printf("Bank transfer is in the process.\n"); } -} - - +} \ No newline at end of file From 80646bffae94e58800e50f93334dc6f5ef186b5f Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Oct 2018 21:00:01 -0400 Subject: [PATCH 8/8] Working Project; all Tasks accomplished. Comments included to help us demo --- src/edu/temple/cis/c3238/banksim/TransferThread.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/edu/temple/cis/c3238/banksim/TransferThread.java b/src/edu/temple/cis/c3238/banksim/TransferThread.java index 7c9b8eb..37b0f38 100644 --- a/src/edu/temple/cis/c3238/banksim/TransferThread.java +++ b/src/edu/temple/cis/c3238/banksim/TransferThread.java @@ -23,5 +23,8 @@ public void run() { int amount = (int) (maxAmount * Math.random()); bank.transfer(fromAccount, toAccount, amount); } + + /* when one bank closes, it calls closeBank (closing them all) */ + bank.closeBank(); } }