aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding getId Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding getId" Watch "Regarding getId" New topic
Author

Regarding getId

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69

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

the answer is C and D
But im unable to understand how we get 4 2 and 4 4..how this Id is obtained
PLEASE EXPLAIN IN DETAIL
Norbert Muench
Greenhorn

Joined: Mar 09, 2012
Posts: 19
All you need to need to know about thread ids to answer this mock question is stated in the Javadoc for the getId() method in class java.lang.Thread:
Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

You should start by asking yourself which of the given lines will even compile. (This will eliminate one line)
Then ask yourself which of the remaining lines actually output two numbers. (This will eliminate another line)
Then you should ask yourself what it means when the output is "4 4". How many threads did the program start in this case?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40


Please QuoteYourSources.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Henry Wong wrote:
Please QuoteYourSources.

Henry


SCJP K & B
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hint:
new Starter(). run() and new Thread(new Starter()).run () do not create any thread. The main thread execute the run methods in both cases. Assume the main thread's ID is 4.
Katherine Reut
Greenhorn

Joined: Jul 26, 2013
Posts: 28
Norbert Muench wrote:All you need to need to know about thread ids to answer this mock question is stated in the Javadoc for the getId() method in class java.lang.Thread:
Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

You should start by asking yourself which of the given lines will even compile. (This will eliminate one line)
Then ask yourself which of the remaining lines actually output two numbers. (This will eliminate another line)
Then you should ask yourself what it means when the output is "4 4". How many threads did the program start in this case?


To expand on Norbert's, and the author's, answers:

"Fragment I doesn’t start a new Thread."
This means that the Ids that are produced will be the same. (i.e. “2 2” or “4 4”)

"Fragment II doesn’t compile."
This means that there will be no output, and that “A” cannot be true.

"Fragment III creates a new thread and doesn’t start it."
This means that there will be only one Id output. (i.e "2" or "4")

"Fragment IV creates a new thread and invokes run( ) directly, but doesn’t start the new thread."
This means that the Ids that are produced will be the same. (i.e. “2 2” or “4 4”).

"Fragment V creates and starts a new thread."
This means that the Ids that are produced will be different. (i.e. “4 2”).

There's also a little information about getId() in the Exam Watch on page 716.
 
 
subject: Regarding getId