• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic