



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































2 This program runs threads that deposit and withdraw 3 money from the same bank account. 4 */ 5 public class BankAccountThreadRunner 6 { 7 public static void main(String[] args) 8 { 9 BankAccount account = new BankAccount(); 10 final double AMOUNT = 100; 11 final int REPETITIONS = 100; 12 final int THREADS = 100; 13 14 for (int i = 1; i <= THREADS; i++) 15 { 16 DepositRunnable d = new DepositRunnable( 17 account, AMOUNT, REPETITIONS); 18 WithdrawRunnable w = new WithdrawRunnable( 19 account, AMOUNT, REPETITIONS); 20
BankAccountThreadRunner.java (cont.)
21 Thread dt = new Thread(d); 22 Thread wt = new Thread(w); 23 24 dt.start(); 25 wt.start(); 26 } 27 } 28 }
DepositRunnable.java (cont.)
25 public void run() 26 { 27 try 28 { 29 for (int i = 1; i <= count; i++) 30 { 31 account.deposit(amount); 32 Thread.sleep(DELAY); 33 } 34 } 35 catch (InterruptedException exception) {} 36 } 37 }
2 A withdraw runnable makes periodic withdrawals from a bank account. 3 / 4 public class WithdrawRunnable implements Runnable 5 { 6 private static final int DELAY = 1; 7 private BankAccount account; 8 private double amount; 9 private int count; 10 11 /* 12 Constructs a withdraw runnable. 13 @param anAccount the account from which to withdraw money 14 @param anAmount the amount to withdraw in each repetition 15 @param aCount the number of repetitions 16 */ 17 public WithdrawRunnable(BankAccount anAccount, double anAmount, 18 int aCount) 19 { 20 account = anAccount; 21 amount = anAmount; 22 count = aCount; 23 }
2 A bank account has a balance that can be changed by 3 deposits and withdrawals. 4 / 5 public class BankAccount 6 { 7 private double balance; 8 9 /* 10 Constructs a bank account with a zero balance. 11 */ 12 public BankAccount() 13 { 14 balance = 0; 15 } 16
BankAccount.java (cont.)
18 Deposits money into the bank account. 19 @param amount the amount to deposit 20 */ 21 public void deposit(double amount) 22 { 23 System.out.print("Depositing " + amount); 24 double newBalance = balance + amount; 25 System.out.println(", new balance is " + newBalance); 26 balance = newBalance; 27 } 28
BankAccount.java (cont.)
Program Run:
Depositing 100.0, new balance is 100. Withdrawing 100.0, new balance is 0. Depositing 100.0, new balance is 100. Withdrawing 100.0, new balance is 0. ... Withdrawing 100.0, new balance is 400. Depositing 100.0, new balance is 500. Withdrawing 100.0, new balance is 400. Withdrawing 100.0, new balance is 300.
Create two sets of threads:
public void deposit(double amount) { System.out.print("Depositing " + amount); double newBalance = balance + amount; System.out.println(", new balance is "
Depositing 100.0, new balance is 100. Withdrawing 100.0, new balance is 0. Depositing 100.0, new balance is 100. Depositing 100.0, new balance is 200. Withdrawing 100.0, new balance is 100. ... Withdrawing 100.0, new balance is 0.
Depositing 100.0Withdrawing 100.0, new balance is 100.0, new balance is -100.
The balance variable is now 100 instead of 0 because the deposit method used the old balance to compute the value of its local variable newBalance
Corrupting the Contents of the balance Variable
21.4 Synchronizing Object Access
To solve problems such as the one just seen, use a
lock object
Lock object: used to control threads that manipulate
shared resources
In Java library: Lock interface and several classes that
implement it
ReentrantLock: most commonly used lock class
Locks are a feature of Java version 5.
Earlier versions of Java have a lower-level facility for thread
synchronization
Synchronizing Object Access (2)
Typically, a Lock object is added to a class whose
methods access shared resources, like this:
public class BankAccount
{
private Lock balanceChangeLock;
... public BankAccount()
{
balanceChangeLock = new ReentrantLock();
...
}
...
}