• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about threads.  RSS feed

 
Javier Giron
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

Im preparing the OCP Java 7 certification and i have the next doubt about a question in a book i bought.

Given:



And given the following five fragments:
I. new Starter.run(); NO COMPILA!
II. new Starter.start(); NO COMPILA!
III. new Thread(new Starter()); COMPILA, PERO NO HACE NADA!
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.
D. Exactly three might produce the output 4 4.
E. Exactly three might produce the output 4 2.

And the answer give by the book is:
C and D are correct...


Ok, so i understand that fragments I, II and III dont compile or dont do anything.
My doubt comes when i see IV and V.

In case of fragment IV what i see is that the code prints first the ID of the MAIN thread, for example 4. And when it calls "new Thread(new Starter()).run()" is creating a new Thread but not starting it. So its still in the MAIN thread, so it should print again the same number, in this case 4, which it is option B.

In case of fragment V is creating a new Thread and starting it, so it should print a different number, for example 2. So C will be also correct.

Can anyone explain me where is the mistake? I also checked this in Eclipse and for what was printed i think im right and maybe is a book´s mistake, or maybe i just misunderstood this topic about threads.

Thanks to all!
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case that you are copying material from a book, please list the book information and what page it is found on.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so i understand that fragments I, II and III dont compile or dont do anything.

Why 1 not compile or do nothing?
sorry overlook that new Starter.run(); wrong creation of object, or trying call static method.
may be errata if new Starter().run(); answer of book will be correct
 
Javier Giron
Greenhorn
Posts: 20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book is "OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804) (Certification Press)" by Kathy Sierra and Bert Bates. Chapter 13, question 13 in the Self Test. I bought the ebook, so i cant tell you, exactly, in which page is the question is.

Sergei, you were right, i misscopy the code and i forgot the brackets in the first two cases. Now, everything is clear. Спасибо большое!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!