This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This is what happens...
Thread1 comes in first(lets assume) inside the loop, gets the lock, calls getNumber() , releases the lock & immediately gets the lock again and calls setNumber() (because getNumber() is called in the argument.JVM after solving the argument will directly call the method).
This may continue for some time. We can determine it. Depends on the OS. Lets say i=55 has completed, the set number is 55 now.
When the context is switched on to Thread2, it enters its own loop with i=1. It now gets the lock, calls getNumber() (which is now going to return 55 as the Account object is common to both the threads), calls setNumber().
This may again go on for some time. Lets say when thread2's i=25, number in Account is 70.
Steps 1 to 4 goes on again...
i have looked at the answer but i still have a Doubt
It says that there occurs context switch.
if such is the case then why doenot it happen in the code that uses iincrease() Then,both the programs should return false