File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stoian Azarov
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you
 
Stoian Azarov
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:I agree with you


Thanks, I am a bit more calm now!
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
dennis deems
Ranch Hand
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too have encountered contrary output with fragment I, where the first output was "dude".
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for all opinions.
 
Jigar Rathod
Greenhorn
Posts: 2
Eclipse IDE Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • 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.!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic