I am studying for the OCP java Programmer and I am trying to do an test on the Study Guide but I can't understand its solution.
The test is the number 15, Chapter 13 (page 778) but I report it below:
The corretc answer says: the first output must be yo.
I really can't understand why.
Why can't the second Thread enter in the "while" and print "dude" before the first Thread (which changed the flag value)?
I am looking forward to hearing from you.
posted 1 year ago
That should always print "yo" if chat() method is synchronized. I agree that with this implementation, it can print dude as well at the first place.