This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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!
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.
Joined: May 11, 2004
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.