BA3 - */ public synchronized void withdraw(double amount)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { /** Constructs a bank account with a zero balance */ public BankAccount() { balance = 0; } /** Deposits money into the bank account. @param amount the amount to deposit */ public synchronized void deposit(double amount) { System.out.println(Thread.currentThread().getName() + " depositing " + amount); double newBalance = balance + amount; //Waste some time int n = 1 ; for (int i = 0 ; i < 1000000 ; i ++) { n = i * i ; n = i * i ; } System.out.println(Thread.currentThread().getName() + " says new balance is " + newBalance); balance = newBalance; notifyAll() ; } /** Withdraws money from the bank account. @param amount the amount to withdraw
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: */ public synchronized void withdraw(double amount) throws InterruptedException { while (balance &lt; amount) { System.out.println(Thread.currentThread().getName() + &quot; waiting.&quot;) ; wait() ; System.out.println(Thread.currentThread().getName() + &quot; released.&quot;) ; } System.out.println(Thread.currentThread().getName() + &quot; withdrawing &quot; + amount); double newBalance = balance - amount; System.out.println(Thread.currentThread().getName() + &quot; says new balance is &quot; + newBalance); balance = newBalance; } /** Gets the current balance of the bank account. @return the current balance */ public double getBalance() { return balance; } private double balance; }...
View Full Document

This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

Page1 / 2

BA3 - */ public synchronized void withdraw(double amount)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online