Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

How does the getName in Thead works ?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,

Just started learning Threads for my certification but my curiosity drove me beyond certificaton objectives.

I tried to find the Thread class source code but could not fully understand.

in my PSVM(test class - public static class void main) method, I am coding the below code (where class B extends Thead)
B b = new B("b");
Compiler forces me to use the below constructor
B(String s) {} //It does not force me to pass s to the super constructor

My question is

super() statement is added by default, when we inherit. I also know there is a no-arg constructo in Thread class, so compiler wont complain. So, all Ok, untill now.
but how does b.getName() returns "b" ?
Constructor of B don't even pass "b" to super class, so super class dont know anything about the name "b", so it returns "Thread-0" in my JVM implementation.  
However, If I add super(s) statement inside B's constructor, I get the name displayed as b.

I am new to java, but I am missing some fundamentals here. Please help me.

b.getName() for sure refers to the method in Thread class (as B extends Thread, and class B has not overridden getName() method). at somepoint later, If I execute the statment, b.getName(), I get "b". I actually expect "Thread-0". I am wondering, how did the name instance b is aware of his name "b".  (in otherwords how was the name passed  up in the hierarchy in constructors).

This is interesting topic to me, but I might not be clear. So, to be clear, I paste my playing code here, so you get a picture of me. I have lot of commented code, so please bear with me


 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Vinay,

if I run your code, then this is the output that I get (for technical reasons I had to name your classs B as B2):

Inside Run method of Class B2
main
Thread name : main  ,  1  ,  5  ,  true  ,  RUNNABLE
Thread name : t  ,  10  ,  5  ,  false  ,  NEW
Thread name : Thread-0  ,  11  ,  5  ,  false  ,  NEW

so I do get the expected result. Can you elaborate a little on your outcome of "b"?
 
Vinay Kumar T M
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My bad. I think I have overlooked it. Thanks for the help.

I see the correct output.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic