• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Thread Q from Dan's study guide

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

Q: What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: [T1,A][T2,B].
c. Prints: [T1,B][T2,B].
d. Prints: [T2,B][T1,A].
e. Prints: [T2,A][T1,A].
f. Compiler error.
g. Run time error.
h. None of the above.
Answer is b c d e.
Expl: Since method m1 is not synchronized there is no guarantee that any one thread will complete the method before another begins. Since the behavior of Thread.yield is implementation dependent there is no guarantee that control will be transferred to another thread. Even though the start method is invoked on thread T1 first there is no guarantee that it will actually begin to run first.
My Question : I was expecting b or d since the order can't be garunteed. What threw me off is T1,B and T2,A can happen. Isn't T1'S1 = A and T2's S1 = B. Where am I going wrong?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Monisha,
Suppose that T1 invokes b.m1 first. T1 will set b.s1 to A. If the yield method causes T1 to yield to T2, then b.s1 will be set to B before the print statement runs.
 
Monisha Talwar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan.
 
You didn't tell me he was so big. Unlike this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic