Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread question

 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.");
}
}
}
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cleaning up to keep on topic...
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joyce Lee,
thank you very much for your neat explanation.
 
Rahul Bhosale
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thats chear...then, why is it that making the withDraw method of Account class not solving the problem either..?
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic