wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 13 chapter 9 of k&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question 13 chapter 9 of k&B" Watch "Question 13 chapter 9 of k&B" New topic

Question 13 chapter 9 of k&B

Mauricio Archbold Babroza

Joined: Apr 01, 2009
Posts: 11

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???

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...

Greg Beresnev

Joined: Jun 05, 2010
Posts: 2
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
jQuery in Action, 3rd edition
subject: Question 13 chapter 9 of k&B
It's not a secret anymore!