zheng li wrote:
I know the answer is F.
What I don't understand is the explanation.
Why "the first output must be yo with either fragment.
I think if it is the fragment II, it is very possible that the output is dude dude yo yo.
Mo Jay wrote:Hi Guys,
There is NO way that dude will printed first in this code because you have 2 threads that are NOT interfering with each other because each one of them has his own lock. They are NOT acquiring the same lock on the same object, therefore each one of them is executing chat() method individually from top to bottom.
In order to have dude printed first you need to lock both threads on the same object, and below is an example of the code snippet that will do it:
DudesChat dc = new DudesChat();
Thread t1 = new Thread(dc);
Thread t2 = new Thread(dc);
t1.start();
t2.start();
Hope this helps clarify the issue.
Cheers!!!
Mo Jay wrote:Hi Guys,
There is NO way that dude will printed first in this code because you have 2 threads that are NOT interfering with each other because each one of them has his own lock. They are NOT acquiring the same lock on the same object, therefore each one of them is executing chat() method individually from top to bottom.