This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15" Watch "Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15" New topic
Author

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

Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Jan 28, 2008
Posts: 5575

I agree with you
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Seetharaman Venkatasamy wrote:I agree with you


Thanks, I am a bit more calm now!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Mar 12, 2011
Posts: 808
I too have encountered contrary output with fragment I, where the first output was "dude".
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Thanks very much for all opinions.
Jigar Rathod
Greenhorn

Joined: Oct 27, 2012
Posts: 2

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.!


Jigarkumar Rathod,
Junior Software Engineer
(Oracle Certified Java Programmer)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15
 
Similar Threads
Thread self test SCJP6
Question 15 chapter 9 of k&B
Non synchronized method
K&B chapter 9 Threads question 15
about Thread(ch09 q15)