aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question of SCJP Guide by Kathy Sierra - Chapter Threads 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 of SCJP Guide by Kathy Sierra - Chapter Threads" Watch "Question of SCJP Guide by Kathy Sierra - Chapter Threads" New topic
Author

Question of SCJP Guide by Kathy Sierra - Chapter Threads

Tere Luna
Greenhorn

Joined: Jul 19, 2011
Posts: 12
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
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
There are two potential threads: main and the new Thread that might be created and started at line 9.

You don't know what the thread id numbers will be, but you do know that they will be different.


V. is the only fragment that successfully starts a second thread and prints two different id numbers. (ANSWER C)

I. and IV. call run() directly without starting a new thread so they print the thread id of main twice. (ANSWER D)

II. won't compile because a Runnable has no start method, a Thread does.

III.
doesn't start the Thread or call run() so it only prints one id number.
Tere Luna
Greenhorn

Joined: Jul 19, 2011
Posts: 12
Thank you Cole for your answer I think I understand now
Cole Tarbet
Ranch Hand

Joined: Aug 02, 2011
Posts: 30
Glad I could help. I am preparing for the test as well so good luck!
Tere Luna
Greenhorn

Joined: Jul 19, 2011
Posts: 12
Good Luck, I am taking the exam on September
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question of SCJP Guide by Kathy Sierra - Chapter Threads