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.
In the following code, after a few tries, I got an output such as this?
I have synchronized the run method so that the first entering thread will have the lock and finish first. But seems like "sandy" entered first and yet, "jason" is the one who finishes first followed by "sandy". Why is this so? Thanks for the help.
What you are trying to do is - acquire a lock on the object while invoking run method.
is same as
However, you are creating two different objects of MyThread class. Each object is eligible to get a lock 'on itself' - that is, the lock acquired by mt1 will be different than that of mt2, and hence both thread won't block each other.