aspose file tools*
The moose likes Threads and Synchronization and the fly likes Doubt in Thread question  from K&B book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Doubt in Thread question  from K&B book" Watch "Doubt in Thread question  from K&B book" New topic
Author

Doubt in Thread question from K&B book

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

Where is the go() method declared?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Sorry that was my mistake.

I edited the code .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18988
    
  40

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


It is possible.

Henry
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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

Joined: Oct 12, 2007
Posts: 56
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.


SCJP 1.4
Adam Michalik
Ranch Hand

Joined: Feb 18, 2008
Posts: 128
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

Joined: Sep 19, 2008
Posts: 385
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

Joined: Feb 18, 2008
Posts: 128
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
 
subject: Doubt in Thread question from K&B book