File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question " Watch "Thread question " New topic
Author

Thread question

Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
I'm battling with this code Please help. I starting coding based on the description of a problem given in K&B. Its not an exact lift from the book, as i was trying to do this all by myself. Though i was using the ideas there, the code below is not functioning the way its supposed to.


the o/p
Fred about to make a withdrawal. Account Balance is : 50.0
Lucy about to make a withdrawal. Account Balance is : 50.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 40.0
Fred about to make a withdrawal. Account Balance is : 40.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 30.0
Lucy about to make a withdrawal. Account Balance is : 30.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 20.0
Lucy about to make a withdrawal. Account Balance is : 20.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 10.0
Fred about to make a withdrawal. Account Balance is : 10.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 0.0
The account was over drawn. The new balance is 0.0
Lucy about to make a withdrawal. Account Balance is : 0.0
Insufficient funds.
The account was over drawn. The new balance is 0.0
Lucy about to make a withdrawal. Account Balance is : 0.0
Insufficient funds.
The account was over drawn. The new balance is 0.0
Fred successfully withdrew 10.0 dollars. Account Balance is : -10.0
The account was over drawn. The new balance is -10.0
Fred about to make a withdrawal. Account Balance is : -10.0
Insufficient funds.
The account was over drawn. The new balance is -10.0
Fred about to make a withdrawal. Account Balance is : -10.0
Insufficient funds.
The account was over drawn. The new balance is -10.0


The account should never have gone negative because of the synchronized option is used in the method signature



Something is terribly wrong in the code somewhere.... Can somebody please help me...
[ April 11, 2005: Message edited by: Barry Gaunt ]

RB
Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
synchronize on account in makeWithDrawal method and check the output

private void makeWithDrawal(double amount) {
synchronized(account){
double newBalance = account.getBalance();
System.out.println(Thread.currentThread().getName() + " about to make a withdrawal. " + account);
if(account.getBalance() >= amount){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
account.withDraw(amount);
System.out.println(Thread.currentThread().getName() + " successfully withdrew " + amount + " dollars. " + account);
}else{
System.out.println("Insufficient funds.");
}
}
}


SCJP 1.4, SCBCD 1.3
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
I was trying to solve a problem description from the Kathy Sierra & Bert Bates ' (K&B in short) book on Java Certification.
I solved the problem by changing the run method.


[ April 11, 2005: Message edited by: Barry Gaunt ]
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
Soni,
thanks for posting a solution.. so happened that i too tried the same.. and worked. Thanks.



Anyway, coming to the current problem, why is it that marking the method
synchronized does not solve the problem?
thanks in advance for your responses.
[ April 11, 2005: Message edited by: Barry Gaunt ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi RB,

Anyway, coming to the current problem, why is it that marking the method
code:

private void makeWithDrawal(double amount)

synchronized does not solve the problem?


When synchronizing the makeWithDrawal, it only prevents other threads from accessing the synchronized methods of a ThreadedAccount object concurrently, not the Account object. But here, there are two ThreadedAccount objects, fred and lucy, and each thread is accessing the synchronized method of its respective ThreadedAccount object concurrently and that's why the account is overdrawn

Joyce
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Cleaning up to keep on topic...


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
Joyce Lee,
thank you very much for your neat explanation.
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
Thanks Barry,
thanks for changing the text in the topic.

I was wondering if there was a way to change the text in the topic.. so looks like an Administrator function.. is it?
Krishnakumar
Greenhorn

Joined: Mar 15, 2005
Posts: 22
Ok thats chear...then, why is it that making the withDraw method of Account class not solving the problem either..?
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
I need to clarify below things,
1. Fred & Lucy has different accounts and has the account balance of 50.
2. Should not withdraw more than 10
3. Account balance should not reduce below 10 i.e it should not be 0 or negative.
Using the above spec i have made the following changes in your code.





Output:

Fred about to make a withdrawal. Account Balance is : 50.0
Lucy about to make a withdrawal. Account Balance is : 50.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 40.0
Fred about to make a withdrawal. Account Balance is : 40.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 40.0
Lucy about to make a withdrawal. Account Balance is : 40.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 30.0
Fred about to make a withdrawal. Account Balance is : 30.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 30.0
Lucy about to make a withdrawal. Account Balance is : 30.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 20.0
Fred about to make a withdrawal. Account Balance is : 20.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 20.0
Lucy about to make a withdrawal. Account Balance is : 20.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 10.0
Fred about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Lucy successfully withdrew 10.0 dollars. Account Balance is : 10.0
Lucy about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.


Hope it is useful to u


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
RB: I was wondering if there was a way to change the text in the topic.. so looks like an Administrator function.. is it?

It's possible to change the Subject if he/she is the initiator of the thread by clicking on the pencil/paper icon of the first post. See JavaRanch FAQ: Editing Your Posts for details.

Joyce
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
Raghu Shree,
But the idea is to have 2 people access the same account. If it were 2 separate accounts, the issue of synchronization doesnt arise at all. Isnt it? Or did i miss something there. I think the correct solution posted by Joyce Lee appears to be the best unless somebody has a different way to solve it.
KrishnaKumar
why is it that making the withDraw method of Account class not solving the problem either..?
I'm assuming you mean that making the withDraw method of Account class synchronized. If you make that method synchronized, you still have a state of the object which could return incorrect information to different threads.
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
This also works fine. but I dpn't know its satisfy your requirements.

Output

Fred about to make a withdrawal. Account Balance is : 50.0
Lucy about to make a withdrawal. Account Balance is : 50.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 40.0
Fred about to make a withdrawal. Account Balance is : 40.0
Lucy successfully withdrew 10.0 dollars. Account Balance is : 30.0
Lucy about to make a withdrawal. Account Balance is : 30.0
Fred successfully withdrew 10.0 dollars. Account Balance is : 20.0
Fred about to make a withdrawal. Account Balance is : 20.0
Insufficient funds.
Lucy about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Fred about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Lucy about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Fred about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Lucy about to make a withdrawal. Account Balance is : 10.0
Insufficient funds.
Insufficient funds.

Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
Raghu Shree,
Thanks for your time, but there is a logical error in your code. Anyway, the idea behind this post is to understand how 2 threads behave on shared resources and how to avoid inconsistent states of shared resources. The idea was not to fix a banking application bug.
thanks to all who responded positively particularly Joyce Lee.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question