• 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

Doubt about Self Test Chapter 13, Question 15 (CH13Q15, K&B7)

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 13, p 778 (question 15)

The answer that is stated as being correct is F, but also this statement is wrong:

if chat isn't synchronized (code fragment II) it's still impossible to have the output yo dude dude yo:

Both threads have access to the flag variable but this variable is static so they have access to the same 'instance' of this variable.
Both check whether the flag is set to '0', which is it's original value (upon loading of the class).

When the first thread (I'll call it t1) enter 'chat' (unsynchronized) execution can swap when the first thread is inside the 'if' body, the other thread (t2) can enter the method and change the value to its own id.

"yo " is outputted by t2
again swapping of threads
t1 changes the value of flag
again swap
t2 outputs "dude "

and then, in NO POSSIBLE WAY there can be another "dude " output!!!

-> the for loop in which t2 outputted "yo " and "dude " is finished, so t2 runs to the dead state

-> t1 just finished putting flag = id, so its output will be "yo " and "yo ", t2 doesn't change the value of flag anymore

so "yo dude dude yo" in answer F should be "yo dude yo yo"

kind regards,

S.
 
Ranch Hand
Posts: 57
1
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sander Theetaert wrote:Chapter 13, p 778 (question 15)
so "yo dude dude yo" in answer F should be "yo dude yo yo"



Hi Sander,
Answer F is correct as well as yours: With fragment I, the output could be yo dude dude yo.

There are in fact several different variations of output:
- There are at least two "yo"s.
- The order of the different tokens is not guaranteed.

Regards,
Bernhard
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sander Theetaert,

First of all, a warm welcome to CodeRanch!

Sander Theetaert wrote:The answer that is stated as being correct is F, but also this statement is wrong:

if chat isn't synchronized (code fragment II) it's still impossible to have the output yo dude dude yo


First of all, threading questions are probably the hardest to answer on the actual exam and to write/test in mock questions/exams. In answer F (With fragment II, the output could be yo dude dude yo), the word could is very important. Because if you run the application 10 times and just once it prints yo dude dude yo, then answer F is correct.

With fragment II (chat isn't synchronized) I was able to have the program print yo dude dude yo. I adjusted the Dudes class a bit by adding a few Thread.sleep invocations with 10 milliseconds to (try to) swap both threads.When I run the DudesChat application with the adjusted Dudes class a few times, I get these outputs:
dude yo dude yo
yo dude yo dude
yo dude dude yo
yo dude yo dude
dude yo yo dude
yo dude dude yo


So we clearly have a winner! We even have 2 Therefore answer F is the correct answer.

Hope it helps!
Kind regards,
Roel

(PS. With the same technique I was also able to simulate the scenario you described and you are correct as well: when threads are swapped at a different line of code, the output will be yo dude yo yo but that doesn't result in answer F being a wrong answer)
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic