Hi All,
I have a doubt on the below question of the Self Test of Chapter 9 written in the SCJP 6 Guide,
13. Given:
3. public class Starter implements Runnable {
4. void go(long id) {
5. System.out.println(id);
6. }
7. public static void main(
String[] args) {
8. System.out.print(Thread.currentThread().getId() + " ");
9. // insert code here
10. }
11. public void run() { go(Thread.currentThread().getId()); }
12. }
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.)
A. All five will compile
B. Only one might produce the output 4 4
C. Only one might produce the output 4 2
D. Exactly two might produce the output 4 4
E. Exactly two might produce the output 4 2
F. Exactly three might produce the output 4 4
G. Exactly three might produce the output 4 2
Answer:
✓ C and D are correct. Fragment I doesn't start a new thread. Fragment II doesn't compile.
Fragment III creates a new thread but doesn't start it. Fragment IV creates a new thread
and invokes run() directly, but it doesn’t start the new thread. Fragment V creates and
starts a new thread.
A, B, E, F, and G are incorrect based on the above. (Objective 4.1)
Can anyone explain to me why C and D are correct? I don't really understand 