This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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 ---------------------------------------------------------------
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(); }
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
posted
0
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."