• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B chapter 9 Threads question 15

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


And given these two fragments:


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?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rodmar Conde wrote:

So, when using synchronized, the output should be

yo yo yo dude dude dude

or

dude dude dude yo yo yo



You should check the for loop carefully.
 
Rodmar Conde
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Zhiwei Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



That's why the use the term, could be occur. It's a possible solution.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Zhiwei Huang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


That's why I said it's MY BAD and thanks for reminding me the EDIT part.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

phil sohar wrote:

if we use


then output could be



Definitely not. Check it again!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zhiwei Huang wrote:
That's why I said it's MY BAD and thanks for reminding me the EDIT part.



I mean, on the same post.
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?

and if i remove static from line 14,it is giving me a java.lang.NullPointerException.
explain this please.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arjun, check you corresponding post here
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks, Kurt
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic