File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question of SCJP Guide by Kathy Sierra - Chapter Threads

 
Tere Luna
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Cole for your answer I think I understand now
 
Cole Tarbet
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad I could help. I am preparing for the test as well so good luck!
 
Tere Luna
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Luck, I am taking the exam on September
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic