File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread question from K&B book

 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am preparing for the SCJP 6 exam and came across this question from K&B Book



Some of the options are :

I.synchronized void chat(long id)
II. void chat(long id)

1. With fragment I output could be Yo Dude Dude Yo
2. With fragment I output could be Dude Dude Yo Yo
3. With fragment II output could be Yo Dude Dude Yo

The correct answer is 3 which i understood but this statement in the answer confuses me "with either fragment the first output must be Yo".

Why is it not possible to print Dude first then Yo for the Fragment 2?

Please explain.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the go() method declared?

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that was my mistake.

I edited the code .
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is it not possible to print Dude first then Yo for the Fragment 2?


It is possible.

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then could you please explain what they are trying to point out here

"with either fragment the first output must be Yo"
 
Vivek Kr Singh
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Masilamani wrote:Then could you please explain what they are trying to point out here

"with either fragment the first output must be Yo"

Dudes instance created in go() has no synchronization associated with it. There is absolutely no guarantee which thread will execute flag = id first even when using synchronized keyword. It could be any of the threads who enter the block first.
 
Adam Michalik
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it does not explain the doubt (which I'm having, too). Let's consider a following scenario using fragment 2 (no synchronization):
Let T1 and T2 be the threads.
T1 executes:

After this flag == 1.
Then T2 takes the CPU and executes:

flag == 1, so it remains unchanged (assuming that flag's value is flushed).
Then, it executes

where id == 2 and flag == 1, so the output is "dude dude".
Then T1 starts again, printing "yo yo" in the loop. So the output is "dude dude yo yo" altogether. Am I wrong somewhere?
 
Siva Masilamani
Ranch Hand
Posts: 385
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam you are almost right.But the value of id will not affect as it is the method local variable, each thread has its own copy and can not be changed by other thread but the problem lies with the static variable "flag".

Lets consider this scenario



Thread T1 execute the if expression but before it update the flag value with id thread T2 also executes the If expression,at this time both the thread enters the if block and T1 updates the flag with its own id say 1 and T2 updates the flag with id 2.

So when T1 executes the for loop it compares the values of flag with its own copy of ID value which is 1 and it did not match the value of flag as it has been updated by Thread t2 with its own ID.So T1 will print Dude Dude and T2 can print Yo Yo.


This is my understanding only,Please correct me if i am wrong.
 
Adam Michalik
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Siva, I think we're making the same point (I know that the id variables are local - I just skipped the explanation that T1's id = 1 and T2's id = 2). Anyway, for me it's possible for the output to be "dude dude yo yo" - contrary to what the answer would suggest that "with either fragment the first output must be Yo".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic