• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt in Thread question from K&B book

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the go() method declared?

Henry
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry that was my mistake.

I edited the code .
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic