We're sorry, but your post appears to contain abbreviations that we don't like people to use at the Ranch. Because JavaRanch is an international forum, many of our members are not native English speakers. For that reason, it's important that we all try to write clear, standard English, and avoid abbreviations and SMS shortcuts. See here for more of an explanation. Thanks for understanding.
If the abbreviation occurs within code, you can use code tags to post it successfully.
The specific error message is: "r" is a silly English abbreviation; use "are" instead.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:
Are you sure the code is correct?? There is only a balance of 50 in the account so withdrawal of 10 from it 10 times should not be possible.
OCPJP6
Trivikram Kamat wrote:
You may get withdrawal 6 or 7 times rarely if thread switching takes place at line:
balance = balance - amount;
Igor Bo wrote:I think there is something wrong in JVM on Mac, can't explain it another way
Lester Burnham wrote:
Have you tried running the code outside of NetBeans, from the command line?
Igor Bo wrote:But can it be 7 withdrawals?
Lester Burnham wrote:
Igor Bo wrote:But can it be 7 withdrawals?
Note that the check for overdraft happens after the withdrawal - and it causes the thread to sleep.
Also, there is no synchronization of the access to the Account object.
Igor Bo wrote:
Fred is going to withdraw
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Lucy is going to withdraw
Fred completes the withdrawal
Fred is going to withdraw
Lucy completes the withdrawal
Not enough in account for Lucy to withdraw 0
Not enough in account for Lucy to withdraw 0
Fred completes the withdrawal
account is overdrawn!
Not enough in account for Fred to withdraw -10
account is overdrawn!
Igor Bo wrote:
There is no synchronization, so each of 2 threads can ask for the balance simultaneously. To make a withdrawal they need to get at least 10 in balance. So if they ask simultaneously for the balance, get 10 and make withdrawal (2 threads * 10 = 20) the result would be -10 in balance and after that they can NOT make withdrawals anymore.
4 withdrawals to have balance of 10 + 2 withdrawals made simultaneously by threads getting balance of 10 = 6 withdrawals. How can it be 7???
Didn't get note that overdraft happens after the withdrawal. I think it's not important here because we are calculating how many times program(threads) can make withdrawals, right? Or what is the maximum number of lines "**** completes the withdrawal " in the output? **** = Fred | Lucy.
Henry Wong wrote:
This one line isn't atomic. It loads the value of balance into a register, subtracts the value of amount from it, and then stores the value back into memory.
Yeah, but is it art? What do you think tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|