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?
This message was edited 2 times. Last update was at by Zhiwei Huang
Rodmar Conde
Greenhorn
Joined: Jul 09, 2010
Posts: 11
posted
0
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.
This message was edited 1 time. Last update was at by Rodmar Conde
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
posted
0
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...
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.
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.
This message was edited 1 time. Last update was at by Abimaran Kugathasan
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
This message was edited 2 times. Last update was at by phil sohar
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.
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()?
This message was edited 1 time. Last update was at by Abimaran Kugathasan
Kurt Sys
Greenhorn
Joined: Sep 05, 2010
Posts: 1
posted
0
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?