aspose file tools*
The moose likes Threads and Synchronization and the fly likes Mughal SCJP Thread question - the use of super.start() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Mughal SCJP Thread question - the use of super.start()" Watch "Mughal SCJP Thread question - the use of super.start()" New topic
Author

Mughal SCJP Thread question - the use of super.start()

matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 67
This code originated in Mughal/Rasmussen's SE 6 Study Guide page 623 Question 5. I have 1 question embedded in the code comments below. Thanks.

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

Invoking the method "start" and the method "run" are 2 different things.

Invoking start- creates a new thread of execution. So you invoke super.start() and the method checks that run method is overidden in the class that extends Thread and hence invokes that.
From the Javadoc for Thread#start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

So you can see that JVM calls the run method of this thread. So if you have overridden the run method in your extended class then it invokes that overridden version else it would invoke the run method implementation in the Thread class which is shown below in the below code.

whereas, when you invoke run on Thread, the following is the underlying code that gets invoked:


where target is the Runnable which was passed while creating the instance. [Code taken from Thread.java].

So you can see why the behavior is different for invoking start() and run()

Mohamed Sanaulla | My Blog
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 67
Thanks Mohamed. I get the gist but I'm fuzzy on the perspective.

If Animal has sound() and Dog extends Animal and Dog overrides sound()
in Animal and I create Animal a = new Dog(), I understand how
a.sound() invokes the Dog's sound(). That perspective I understand.

But when I'm at the level of Thread (aka, super) looking horizontally
over the landscape, how does super.start() notice it's very own run()
method has been overridden and needs to adhere to the override by an
extending class?

If you want to refer me to Overriding Methods 101, that would be ok.

Thanks again. Matt
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3070
    
  33

Let me just put your code in a different way:


So you can see that instead of invoking start in the constructor, I have used e.start() in the main method. Both are similar as then invoke the start defined by Thread class. So here the thread which you are going to start is "e" of type Extender and hence according to the Javadoc the current thread's run method has to be invoked when the thread is started, which is nothing but the one overridden in Extender class.
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 67
Thanks Mohamed. Very illuminating and helpful.

It seems to me that start(), super.start() and your e.start() all execute the same start() and run() blocks of code.

When the Javadoc says "the current thread's run method has to be invoked when the thread is started", is that statement to be taken literally such that overrides need not be considered?

If so, it seems to me the only issue needed to be resolved is to determine who owns the thread and in my example would it be the Extender instance by virtue of super.start() appearing in the Extender instance's constructor?

Thanks. Matt
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

matt love wrote:When the Javadoc says "the current thread's run method has to be invoked when the thread is started", is that statement to be taken literally such that overrides need not be considered?


It's to be taken literally, which of course means that polymorphism DOES need to be considered. Doing otherwise would violate the rules of Java.

If so, it seems to me the only issue needed to be resolved is to determine who owns the thread and in my example would it be the Extender instance by virtue of super.start() appearing in the Extender instance's constructor?


You're going to have to clarify what you mean by "who owns the thread". That isn't any kind of standard terminology. In your example you have an Extender object which IS a thread. It doesn't "own" a Thread, if that's what you meant by "owns". It IS a Thread.
matt love
Ranch Hand

Joined: Jan 25, 2010
Posts: 67
OK.

Thank you both.

Matt

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mughal SCJP Thread question - the use of super.start()