I am trying to understand the synchronized keyword practically.So,Here's the problem:
Initial balance is 1000.It's a joint account of Robert and Kristen.
Both want to withdraw 1000 from that account at same time from different locations.
Now , here is the flow of program that i want to achieve:
Robert checks balance,see's it's enough
Krsiten checks balance, see it's enough
Now, when Robert wants to withdraw he see's that there no enough balance, so he get's a message "not enough balance"
Kristen and RObert names can be interchanged as we are not sure which one will be picked by jvm to execute first.
Here is my code:
Joined: Aug 31, 2012
I forgot to mention my problem.
I am getting output correctly, but from the above problem what i have achieved is:
Robert checks for balance,see's it's enough
Robert completes withdraw
Kristen checks for enough balance, it's not enough,so she gets a message " cannot withdraw due to Low balance"
I think that although i am getting the output correctly, this is not the correct approach to show the use of synchronized method.I think both should have checked if there's enough balance , for for both it should have told "yes there's enough balance".And then one of them should go for sleep,the other completes the withdrawal and when the second one when withdrawing should get the message "there's not enough balance"