Despite of the method callMe being synchronized, when I run the program, I get the following output:
I would expect one of the threads to enter the callMe() method and stall it, so that the second thread cannot enter it at all! Can someone please help me understand the output? Also, if there is something wrong here, how do I correct it so as to allow only one thread to enter the method?
Cody Long wrote:there are never any gaurentees when it comes to the schedular
Yes, but there are limits on what can actually happen. It doesn't matter how bad the scheduler is... schedulling a thread that doesn't own the lock to execute code that requires the lock, doesn't make the thread execute the code -- a thread that is in a blocked state will just release its timeslice.
Very true but your output also depends on the JVM.
Again true. But that doesn't mean that there is a JVM out there that doesn't honor synchronization locks -- at least one that has been certified as a valid JVM.