• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a little bit confused about this question:



When fragment I or fragment II is inserted in line 4, which are true? (Choose all that apply.)
A. An exception is thrown at runtime.
B. With fragment I, compilation fails
C. With fragment II, compilation fails
D. With fragment I, the output could be: yo dude dude yo
E. With fragment I, the output could be: dude dude yo yo
F. With fragment II, the output could be: yo dude dude yo

Answers:
F is correct. With fragment I, the chat method is synchronized, so the two threads can't swap back and forth. With either fragment, the first output must be yo.
A, B, C, D and E are incorrect based on the above.


In the book it is stated that the correct answer is F(and part of it is): With either fragment, the first output must be yo.

I have made some test because I couldn't explain to myself why is it certain that "yo" will be the first output and received output that is in contrary: dude dude yo yo.
My assumption is that if the second fragment is used(without synchronization) it is not possible to predict the output. Am I wrong here?

If the first thread stores its id in the flag var and right after that the second starts executing the chat method it would not print yo as a first statement.
I would be really grateful if someone can explain to me what am I missing here?

Regards
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:I agree with you



Thanks, I am a bit more calm now!
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's ESSENTIAL that you post the entire mock question when you want to discuss it. No matter where you got the question, you MUST post the entire question. (That includes the entire answer too!)

Thank you

Bert
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Bert,
I've edited my first post with the full mock question and the given answer. I would not do that same mistake from now on.

Regards
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too have encountered contrary output with fragment I, where the first output was "dude".
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys...to the best of my knowledge...This program as-it-stands the first output could be dude, that depends on the decision of the scheduler in the JVM.
If both threads have concurrent access to the non-synchronized chat method, there's NO guarantee that line 8 will always return true -at the first instance-, hence there is a possibility that the first output is "dude".

Regards

Ikpefua
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much for all opinions.
 
Greenhorn
Posts: 2
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No matter which thread gets first chance flag=0
So output will start with yo

If we select Fragment-1 : synchronized void chat(long id)
output would be yo yo dude dude

If we select Fragment-2 : void chat(long id)
output will start with yo for sure.!!!

So F option is correct.!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic