Skip to content

Commit c84e1ff

Browse files
done
0 parents  commit c84e1ff

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

BankAccount.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.concurrent.locks.Condition;
2+
import java.util.concurrent.locks.Lock;
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
public class BankAccount {
6+
7+
private double balance;
8+
private Lock balanceChangeLock;
9+
private Condition sufficientFundsCondition;
10+
11+
public BankAccount(){
12+
balance = 0;
13+
balanceChangeLock = new ReentrantLock();
14+
sufficientFundsCondition = balanceChangeLock.newCondition();
15+
}
16+
17+
public void deposit(double amount){
18+
19+
balanceChangeLock.lock();
20+
try{
21+
22+
System.out.print("Depositing " + amount);
23+
double newBalance = balance + amount;
24+
System.out.println(", new balance is " + newBalance);
25+
balance = newBalance;
26+
sufficientFundsCondition.signalAll();
27+
28+
}catch(Exception exception){
29+
30+
}finally{
31+
balanceChangeLock.unlock();
32+
}
33+
34+
}
35+
36+
public void withdraw(double amount){
37+
38+
balanceChangeLock.lock();
39+
try{
40+
41+
while (balance < amount) {
42+
sufficientFundsCondition.await();
43+
44+
}
45+
46+
System.out.print("Withdrawing " + amount);
47+
double newBalance = balance - amount;
48+
System.out.println(", new balance is " + newBalance);
49+
balance = newBalance;
50+
51+
}catch(Exception exception){
52+
53+
}finally{
54+
balanceChangeLock.unlock();
55+
}
56+
57+
}
58+
59+
public double getBalance(){
60+
return balance;
61+
}
62+
63+
}

BankAccountThreadRunner.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class BankAccountThreadRunner{
2+
3+
public static void main(String[] args) {
4+
5+
BankAccount account = new BankAccount();
6+
final double AMOUNT = 100;
7+
final int REPETITIONS = 100;
8+
final int THREADS = 100;
9+
10+
for(int i = 1; i <= THREADS; i++){
11+
12+
DepositRunnable deposit = new DepositRunnable(account, AMOUNT, REPETITIONS);
13+
14+
WithdrawRunnable withdraw = new WithdrawRunnable(account, AMOUNT, REPETITIONS);
15+
16+
Thread depositThreat = new Thread(deposit);
17+
Thread withdrawThread = new Thread(withdraw);
18+
19+
depositThreat.start();
20+
withdrawThread.start();
21+
}
22+
23+
}
24+
}

DepositRunnable.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class DepositRunnable implements Runnable{
2+
3+
private static final int DELAY = 1;
4+
private BankAccount account;
5+
private double amount;
6+
private int count;
7+
8+
public DepositRunnable(BankAccount anAccount, double anAmount, int aCount){
9+
account = anAccount;
10+
amount = anAmount;
11+
count = aCount;
12+
}
13+
14+
@Override
15+
public void run() {
16+
try{
17+
for(int i = 1; i <= count; i++){
18+
account.deposit(amount);
19+
Thread.sleep(DELAY);
20+
}
21+
}
22+
catch (InterruptedException exception){
23+
24+
}
25+
}
26+
27+
}

WithdrawRunnable.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class WithdrawRunnable implements Runnable {
2+
3+
private static final int DELAY = 1;
4+
private BankAccount account;
5+
private double amount;
6+
private int count;
7+
8+
9+
public WithdrawRunnable(BankAccount anAccount, double anAmount, int aCount){
10+
account = anAccount;
11+
amount = anAmount;
12+
count = aCount;
13+
}
14+
15+
@Override
16+
public void run() {
17+
18+
try{
19+
for(int i = 1; i <= count; i++){
20+
account.withdraw(amount);
21+
Thread.sleep(DELAY);
22+
}
23+
}
24+
catch(InterruptedException exception){}
25+
26+
27+
}
28+
29+
}

0 commit comments

Comments
 (0)