• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding getId

 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
kiruthigha rajan
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Please QuoteYourSources.

Henry


SCJP K & B
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic