|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rodmar Conde wrote:
So, when using synchronized, the output should be
yo yo yo dude dude dude
or
dude dude dude yo yo yo
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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!
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?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
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...
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
SCJP6.0,My blog Ranchers from Delhi
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.
phil sohar wrote:
if we use
then output could be
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Zhiwei Huang wrote:
That's why I said it's MY BAD and thanks for reminding me the EDIT part.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |