Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP- Thread Problem (K&B 7)  RSS feed

 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear team,
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.

Thank you
Nicola
 
Ashish Bhargava
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!