This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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???
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 ... ...
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