Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in using Synchronized keyword correctly.

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Robert sleeps
Krsiten checks balance, see it's enough
Krsiten withdraws
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:


 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the whole point of synchronizing the method is so that it can't possibly happen. if you don't synchronize it it could possibly do what you say, but remember there is no guarantee
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic