Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

k & b , Ryan and Monica problem......

 
vianyrajnish rajnish
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is the Ryan and Monica problem ( threads , from K&B head first java book) ....

can anyone please tell me why this statement is printing many a times..
[-------------------------------
"Monica completes the withdraw
---> 5
sorry , not enough money for Monica
---> 6
sorry , not enough money for Monica
---> 7
sorry , not enough money for Monica
---> 8
sorry , not enough money for Monica
---> 9
sorry , not enough money for Monica"
--------------------------------------]
------------------------------------------------------------
program is this
---------------------------------------------------------------


class BankAccount {
private int balance = 100;

public int getBalance()
{
return balance;
}

public void withdraw(int amount)
{
balance = balance - amount ;
}
}

public class RyanAndMonicaJob implements Runnable {

private BankAccount account = new BankAccount();

public static void main(String args[])
{
RyanAndMonicaJob theJob = new RyanAndMonicaJob();
Thread one = new Thread(theJob);
Thread two = new Thread(theJob);
one.setName("Ryan");
two.setName("Monica");
one.start();
two.start();
}

public void run() {
for(int i=0;i<10;i++) {
System.out.println("---> " + i);
makeWithdrawl(10);
if (account.getBalance() < 0) {
System.out.println("overdrawn");
break;
}
}
}

private void makeWithdrawl(int amount) {
if(account.getBalance() >= amount) {
System.out.println(Thread.currentThread().getName() + "is about to withdraw");
try {
System.out.println(Thread.currentThread().getName() + "is going to withdraw");
Thread.sleep(500);
} catch(InterruptedException ex) { ex.printStackTrace(); }
System.out.println(Thread.currentThread().getName() + "woke up");
account.withdraw(amount);
System.out.println(Thread.currentThread().getName() + " completes the withdraw");
}
else {
System.out.println("sorry , not enough money for " + Thread.currentThread().getName());
}
}
}
-----------------------------------------------------------------
out put
----------------------------------------------------------------

output is :
E:\headfirst>java RyanAndMonicaJob
---> 0
Ryanis about to withdraw
Ryanis going to withdraw
---> 0
Monicais about to withdraw
Monicais going to withdraw
Ryanwoke up
Ryan completes the withdraw
---> 1
Ryanis about to withdraw
Ryanis going to withdraw
Monicawoke up
Monica completes the withdraw
---> 1
Monicais about to withdraw
Monicais going to withdraw
Ryanwoke up
Ryan completes the withdraw
---> 2
Ryanis about to withdraw
Ryanis going to withdraw
Monicawoke up
Monica completes the withdraw
---> 2
Monicais about to withdraw
Monicais going to withdraw
Ryanwoke up
Ryan completes the withdraw
---> 3
Ryanis about to withdraw
Ryanis going to withdraw
Monicawoke up
Monica completes the withdraw
---> 3
Monicais about to withdraw
Monicais going to withdraw
Ryanwoke up
Ryan completes the withdraw
---> 4
Ryanis about to withdraw
Ryanis going to withdraw
Monicawoke up
Monica completes the withdraw
---> 4
Monicais about to withdraw
Monicais going to withdraw
Ryanwoke up
Ryan completes the withdraw
---> 5
Ryanis about to withdraw
Ryanis going to withdraw
Monicawoke up
Monica completes the withdraw
---> 5
sorry , not enough money for Monica
---> 6
sorry , not enough money for Monica
---> 7
sorry , not enough money for Monica
---> 8
sorry , not enough money for Monica
---> 9
sorry , not enough money for Monica
Ryanwoke up
Ryan completes the withdraw
overdrawn
----------------------------------------------------------------
end
-----------------------------------------------------------------

Thanks ,
vinay Rajnish
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vianyrajnish rajnish:
... can anyone please tell me why this statement is printing many a times.....


put a break in:



This error took down the first implementation of the 1-800 phone system.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic