• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question of SCJP Guide by Kathy Sierra - Chapter Threads

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Cole for your answer I think I understand now
 
Cole Tarbet
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Luck, I am taking the exam on September
 
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic