• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
matt love
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK.

Thank you both.

Matt

 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic