Docsity

BankAccountThreadRunner.java, Summaries of Java Programming

Toggle sidebar
bg1
BankAccountThreadRunner.java
Page 1
Continued
1 /**
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download BankAccountThreadRunner.java and more Summaries Java Programming in PDF only on Docsity!

BankAccountThreadRunner.java

Continued

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 }

WithdrawRunnable.java

Continued

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 }

BankAccount.java

Continued

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.)

Continued

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.

21.3 Race Conditions

 When threads share a common object, they can conflict

with each other

 Sample program: multiple threads manipulate a bank

account

 Create two sets of threads:

  • Each thread in the first set repeatedly deposits $
  • Each thread in the second set repeatedly withdraws $

Sample Program (2)

 Create a BankAccount object, where deposit and

withdraw methods have been modified to print messages:

public void deposit(double amount) { System.out.print("Depositing " + amount); double newBalance = balance + amount; System.out.println(", new balance is "

  • newBalance); balance = newBalance; }

Sample Program (3)

 Normally, the program output looks somewhat like this:

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.

 The end result should be zero, but sometimes the output

is messed up, and sometimes end result is not zero:

Depositing 100.0Withdrawing 100.0, new balance is 100.0, new balance is -100.

Sample Program (5)

 Scenario to explain problem (cont.):

  1. The deposit thread regains control and picks up where it was iterrupted. It now executes:

System.out.println(", new balance is " + newBalance);

balance = newBalance;

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();

...

}

...

}