File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes k & b , Ryan and Monica problem...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "k & b , Ryan and Monica problem......" Watch "k & b , Ryan and Monica problem......" New topic
Author

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

vianyrajnish rajnish
Ranch Hand

Joined: Apr 22, 2007
Posts: 70
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

Joined: Sep 17, 2006
Posts: 1282
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.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
wood burning stoves
 
subject: k & b , Ryan and Monica problem......