• 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:

Question 13 chapter 9 of k&B

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

And given the following five fragments:
I. new Starter().run();
II. new Starter().start();
III. new Thread(new Starter());
IV. new Thread(new Starter()).run();
V. new Thread(new Starter()).start();
When the five fragments are inserted, one at a time at line 9, which are true? (Choose all that apply.)
these are the answer
C. Only one might produce the output 4 2
D. Exactly two might produce the output 4 4
, but why??? if i run this code the output is 1 8 in my pc???


 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are on the right track. Your output agrees with answer 'C' because
there will be two outputs, one from main() and then a second from run().
The actual numbers can vary because the JVM assigns the ID numbers.
Here's the code with the four fragments that compile.
Jim ... ...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the question I had about question 13 was... when I was doing the self test I answered B and C because I assume no two threads will have the same ID number. So is answer D made possible by the fact that the main thread might finish executing by the time the Starter thread's run method and the print statement in it is executed? Would have been nice if it was clarified, since the explanation of the correct answers just states 'Fragment V creates and starts a new thread'. Or maybe I'm just not very bright
 
reply
    Bookmark Topic Watch Topic
  • New Topic