File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B chapter 9 Threads question 15 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B chapter 9 Threads question 15" Watch "K&B chapter 9 Threads question 15" New topic
Author

K&B chapter 9 Threads question 15

Zhiwei Huang
Greenhorn

Joined: Jul 06, 2010
Posts: 4


And given these two fragments:


When fragment I or fragment II is inserted at line 5, 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 ouput 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

The Official Answer is F, but in my opinion as flag is a static variable, even if instance method chat is marked synchronized, flag could still be "touched" by other thread. Actually when I run this code on my machine multiple times, I never saw D and F, and E always happens. Could someone give me any explanation?
Rodmar Conde
Greenhorn

Joined: Jul 09, 2010
Posts: 11
Even if flag could be "touched", the only place where it can be modified is inside the chat method. Which, if is declared as synchronized can't be executed by two different threads at the same time. One thread has to wait for the other thread to completely execute the method.

So, when using synchronized, the output should be

yo yo yo dude dude dude

or

dude dude dude yo yo yo

Does anybody can confirm this please?

Thanks in advance.


SCJA | SCJP Semi Study Guide in Spanish
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Welcome to JavaRanch! QuoteYourSources when you ask question.

E, F are the correct answer. Remember in DudesChat class you have a static variable d. So think about that.


Got it? If not ask!

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rodmar Conde wrote:

So, when using synchronized, the output should be

yo yo yo dude dude dude

or

dude dude dude yo yo yo


You should check the for loop carefully.
Rodmar Conde
Greenhorn

Joined: Jul 09, 2010
Posts: 11
Thanks Abimaran Kugathasan,

I've checked the for loop and the two possible outputs are:

a) yo yo dude dude

and

b) dude dude yo yo

b) is in option E in the question. I have to guess that it's not marked as correct because option a) always ocurrs. But in fact we don't have any warranty that thread in line 22 will always enter the chat method before thread in line 23. ¿do we?

Zhiwei Huang
Greenhorn

Joined: Jul 06, 2010
Posts: 4
Abimaran Kugathasan wrote:Welcome to JavaRanch! QuoteYourSources when you ask question.

E, F are the correct answer. Remember in DudesChat class you have a static variable d. So think about that.


Got it? If not ask!


my bad. the static flag is set to 0 instead of 9 when initialized.
Thus as d is static, the two threads are calling the same chat method on same instance, as the explanation on K&B's book says, they cannot swap back and forth... so the answer should be only F...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rodmar Conde wrote:Thanks Abimaran Kugathasan,

I've checked the for loop and the two possible outputs are:

a) yo yo dude dude

and

b) dude dude yo yo

b) is in option E in the question. I have to guess that it's not marked as correct because option a) always ocurrs. But in fact we don't have any warranty that thread in line 22 will always enter the chat method before thread in line 23. ¿do we?



That's why the use the term, could be occur. It's a possible solution.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Zhiwei Huang wrote:
my bad. the static flag is set to 0 instead of 9 when initialized.
Thus as d is static, the two threads are calling the same chat method on same instance, as the explanation on K&B's book says, they cannot swap back and forth... so the answer should be only F...


This is NOT fair. When I answered the question, the variable flag is set to 9. You've changed it to 0. If it is 9, the possible answer are E and F.

Because, now it is 9, the only answer is F.

When you EDIT the code, after some one has answered, it's better to put the reason to EDIT, otherwise it'll be ODD.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

remember what start method will do is....
it will only start a new thread and when the thread will initiate itself then it will call
the run method....
so for both



nobody knows which one will call run method first...........

but when they call run method....run method will call chat method with the argument
as some id(id will always be a long value)

if we use


then output could be


please have a look at for loop more closely.......even i got confused 1st time...........please have a look


As there is no synchronization any thread can call this method anytime.......and output
could be anything which is unpredictable

so as you cannot predict the output in second case so one output(which one is confired is F)
if it is wriiteen in the options like
With fragment I, the output could be unpredictable........then you can select that options also

so,,,,,only F is correct


SCJP6.0,My blog Ranchers from Delhi
Zhiwei Huang
Greenhorn

Joined: Jul 06, 2010
Posts: 4
Abimaran Kugathasan wrote:
Zhiwei Huang wrote:
my bad. the static flag is set to 0 instead of 9 when initialized.
Thus as d is static, the two threads are calling the same chat method on same instance, as the explanation on K&B's book says, they cannot swap back and forth... so the answer should be only F...


This is NOT fair. When I answered the question, the variable flag is set to 9. You've changed it to 0. If it is 9, the possible answer are E and E.

Because, now it is 9, the only answer is F.

When you EDIT the code, after some one has answered, it's better to put the reason to EDIT, otherwise it'll be ODD.

That's why I said it's MY BAD and thanks for reminding me the EDIT part.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohar wrote:

if we use


then output could be



Definitely not. Check it again!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Zhiwei Huang wrote:
That's why I said it's MY BAD and thanks for reminding me the EDIT part.


I mean, on the same post.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

when i run it on machine the program given on book
with II
yo yo dude dude
with I
yo dude yo dude
yes as fragment I is not synchronized,out is unpredictable,can be anything.
so with I
D,E are also correct.

ANSWER is F according to the book.
yo dude dude yo. how?

also please tell me at line 14, d instance variable is static,so how can we able to access it from non-static method go()?

and if i remove static from line 14,it is giving me a java.lang.NullPointerException.
explain this please.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Arjun, check you corresponding post here
Kurt Sys
Greenhorn

Joined: Sep 05, 2010
Posts: 1
Hey all,

I got it why only answer F is right, but what about 'dude yo yo dude' or 'dude yo dude yo' or 'dude dude yo yo' in case of fragment II (which are answers not included in the question). If the method is synchronized, it's 'yo yo dude dude'. But if it's not synchronized, can 'dude' be the first string printed? This would be the case if the other thread executes the 'if'-clause only between the 'if'-clause and 'for'-loop of the first thread that called the method.
I couldn't reproduce that answer (by including some Thread.sleep()-stuff in different places in de chat-method), but it should be possible too, right? Let's say we have thread0 and thread1. If the thread0 calls the method first, the flag is set to 0. However, before thread0 enters the loop, thread1 calls the method and since the flag is still 0, the flag is set to 1. Now thread0 starts the loop and prints dude first. I expect a similar result if thread1 enters the method first and thread0 comes in between the if-clause and for-loop of thread1.

I just want to be sure I got the whole thread-stuff right... Can 'dude' be the first string that is printed when the method is not synchronized?

Thanks, Kurt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: K&B chapter 9 Threads question 15
 
Similar Threads
Thread synchronization problem K&B, SCJP 6 - chapter 9, question 15
about Thread(ch09 q15)
Question 15 chapter 9 of k&B
Thread self test SCJP6
Non synchronized method