File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "q on threads" Watch "q on threads" New topic
Author

q on threads

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303

What is the result of attempting to compile and run the program?

a. Prints: T1T1T1
b. Prints: T1T1T2
c. Prints: T1T2T2
d. Prints: T1T2T3
e. Prints: T1T1T3
f. Prints: T1T3T3
g. Compile-time error
h. Run-time error
i. None of the above

wht is the answer to this q and y?
this is a dan's q...however, the explanation is unclear.

[ October 25, 2004: Message edited by: Barry Gaunt ]

SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What did happen when you tried to compile and run the program?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Barry, you are very right in asking what did happen when you try to compile and run, but it is not the question, the problem is how did this result arrives.

JayaSiji, first compile and run the code and if the answer doesn't match with your answer, then try to find out what is the reason behind this answer.

This question is pretty simple, but you need to put some efforts in to this before understanding the complete concept.

Best of Luck
Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Sometimes, Kap, it's a Good Thing to hold back a little until the questioner has shown the initiative to do something themselves. Theoretical Physics I can understand, but not Theoretical Programming.
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
It prints out T1T1T3.

This is both what I expected, and what it did. Note however I wasn't so confident that I posted my response without trying it!

Anyway, why?


We create a new thread (new B(), "T1") which is called T1.

The run method is invoked (on a new stack) with the start call. This new stack, called T1, immediately creates an A and calls run. It does not create a thread, it just calls run, so there is no new stack frame. It is not a new thread, and prints out the current thread name T1.

Next, we create a new thread A, but again we do not call start. We call the run method on the thread. From the sun docs:

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.

So this calls the run method of A- which prints out T1- there is no new stack frame for T2. If you change it to a start() call you get T1T2T3, but since we didn't, we get T1.

Finally, we call the new thread correctly, and create a thread which gets its own stack frame with the start() call. When the run method is executed now it finds the new name of the thread T3.
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
Yes, Barry, you are right!!!
Till the time questioner shows interest in solving the question, there is no use in answering it because he will not be able to understand the solution without working on it himself.

Kaps
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51

If you change it to a start() call you get T1T2T3, but since we didn't, we get T1.


Calling start() on T2 does not necessarily cause T1T2T3 to print. You cannot be certain which thread is going to be executed first. The OS controls that. You could just as easily get T1T3T2


Mark Patrick<br />SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: q on threads